C#';的等价物是什么;s在Java和Scala中的访问修饰符
本文关键字:Scala 访问 等价物 是什么 Java | 更新日期: 2023-09-27 17:57:33
以前在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行为,但也可以指定任何封装包。