[英]What are the c# equivalents of of Java's getClass(), isAssignableFrom(), etc.?
[英]What are the equivalents of C#'s access modifiers in Java and Scala?
以前在C#工作過,我現在花了很多時間在Scala和Java上工作。 這可能令人困惑,因為這三種語言的訪問修飾符使用相似的名稱,但並不總是意味着同樣的事情。
Java和Scala中C#的訪問修飾符的等價物是什么?
以下是Java和Scala中與C#的訪問修飾符最接近的等價物。 在內部(在同一組件內可訪問)的情況下,沒有確切的等價物。 在Java中,您可以限制對同一個包的可訪問性,但是包更直接等同於C#的命名空間,而不是它們對於程序集。
(下表中的“無修飾符”被解釋為應用於類成員。也就是說,在C#類中,沒有修飾符的成員是私有的。頂級類型不是這樣,默認為內部類型。)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| C# | Java | Scala | Meaning |
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| no modifier | private (1) | private | accessible only within the class where it is defined |
| private | | | |
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| protected | -- | protected | accessible within the class and within derived classes (2) |
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| internal | no modifier | private[package_name] | accessible within the same assembly (C#) or within the same package (Java / Scala) (3) |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| protected internal | protected | protected[package_name] | accessible within derived classes (2) and anywhere in the same assembly (C#) or package (Java / Scala) (3) |
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| public | public | no modifier | accessible anywhere |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
(1)在Java中,內部類的私有成員對外部類是可見的,但在C#或Scala中則不然。 在Scala中,您可以說private [X]其中X是獲取Java行為的外部類。
(2)在C#和Scala中,受保護的成員在類中是可見的,如果它是該類的實例或另一個派生類的成員,但如果它是較少派生類的實例的成員則不可見。 (在Java中也是如此,除非由於它位於同一個包中而可以訪問它。)
示例(在C#中):
class Base
{
protected void Foo() {}
void Test()
{
Foo(); // legal
this.Foo(); // legal
new Base().Foo(); // legal
new Derived().Foo(); // legal
new MoreDerived().Foo(); // legal
}
}
class Derived : Base
{
void Test1()
{
Foo(); // legal
this.Foo(); // legal
new Base().Foo(); // illegal !
new Derived().Foo(); // legal
new MoreDerived().Foo(); // legal
}
}
class MoreDerived : Derived
{
void Test2()
{
Foo(); // legal
this.Foo(); // legal
new Base().Foo(); // illegal !
new Derived().Foo(); // illegal !
new MoreDerived().Foo(); // legal
}
}
(3)在Scala中,您可以通過指定最內層的包來獲取Java行為,但您也可以指定任何封閉包。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.