简体   繁体   English

Java和Scala中C#的访问修饰符的等价物是什么?

[英]What are the equivalents of C#'s access modifiers in Java and Scala?

Having previously worked in C#, I now spend a lot of time working in Scala and Java. 以前在C#工作过,我现在花了很多时间在Scala和Java上工作。 This can be confusing, because the three languages use similar names for their access modifiers but don't always mean the same thing. 这可能令人困惑,因为这三种语言的访问修饰符使用相似的名称,但并不总是意味着同样的事情。

What are the equivalents of C#'s access modifiers in Java and Scala? Java和Scala中C#的访问修饰符的等价物是什么?

Here are the closest equivalents to C#'s access modifiers within Java and Scala. 以下是Java和Scala中与C#的访问修饰符最接近的等价物。 In the case of internal (accessible within the same assembly), there is no exact equivalent. 在内部(在同一组件内可访问)的情况下,没有确切的等价物。 In Java you can limit accessibility to the same package, but packages are more directly equivalent to C#'s namespaces than they are to assemblies. 在Java中,您可以限制对同一个包的可访问性,但是包更直接等同于C#的命名空间,而不是它们对于程序集。

("no modifier" in the following table is interpreted as applying to class members. That is, in C# class members with no modifier are private. This is not true of top-level types, which default to internal.) (下表中的“无修饰符”被解释为应用于类成员。也就是说,在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) In Java, private members of an inner class are visible to the outer class, but that's not true in C# or Scala. (1)在Java中,内部类的私有成员对外部类是可见的,但在C#或Scala中则不然。 In Scala, you can say private[X] where X is the outer class to get the Java behavior. 在Scala中,您可以说private [X]其中X是获取Java行为的外部类。

(2) In C# and Scala, a protected member is visible within a class if it's a member of an instance of that class or a further derived class, but not if it's a member of an instance of a less-derived class. (2)在C#和Scala中,受保护的成员在类中是可见的,如果它是该类的实例或另一个派生类的成员,但如果它是较少派生类的实例的成员则不可见。 (The same is true in Java, except where it's accessible due to being in the same package.) (在Java中也是如此,除非由于它位于同一个包中而可以访问它。)

Example (in C#): 示例(在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) In Scala, you can get the Java behavior by specifying the inner-most package, but you can also specify any enclosing package. (3)在Scala中,您可以通过指定最内层的包来获取Java行为,但您也可以指定任何封闭包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM