What is the equivalent of c# the access modifier in Java and scala?

I used to work in c# before, and now I spend a lot of time working in scala and Java This can be confusing because access modifiers in the three languages use similar names, but it doesn't always mean the same thing

What is the equivalent of c# the access modifier in Java and scala?

Solution

The following are the closest equivalents to C #'s access modifiers in Java and scala In the case of internal (accessible within the same component), there is no exact equivalent In Java, you can restrict accessibility to the same package, but packages are more directly equivalent to c#'s namespace than they are to assemblies

(the "no modifier" in the following table is interpreted as applied to class members. That is, in c# class, members without modifiers are private. The top-level type is not the case, and the default is internal type.)

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 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 internal classes are visible to external classes, but not in c# or Scala In Scala, you can say private [x], where x is an external class that gets Java behavior

(2) In c# and Scala, a protected member is visible in a class if it is an instance of that class or a member of another derived class, but not if it is a member of fewer instances of derived classes (the same is true in Java, unless it can be accessed because it is in the same package.)

Example (in 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 Java behavior by specifying the innermost package, but you can also specify any closed package

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>