scala outer class access private members of inner class

In java, we can write the following code:

public class Outer{
public class Inner{
   private int i=5;}
public int getint(){
return new Inner().i;}
}

public class Test{
public static void main    (string[] args){
Outer o = new Outer();
System.out.println(o.getint());
}

The above code can run successfully, which means that in the Java specification, private members of inner classes can be accessed through outer classes, and in Scala, can private members of inner classes be accessed through outer classes? In interactive mode, enter the following code:

 class Outer{
     class Inner{
          private def f(){println("f")}
         class InnerMost{
             f()
         }
     }
     (new Inner).f()
     }

The following error message appears: <console>:18: error: method f in class Inner cannot be accessed in Outer.this.Inner (new Inner).f() In scala, outer classes are not allowed to access private members of inner classes . This experiment illustrates how Java and Scala differ in their specification of outer classes accessing members of inner classes.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324928345&siteId=291194637