![](/img/trans.png)
[英]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.