静态内部类不能直接访问外部类的非静态成员,但可以通过 new 外部类().成员 的方式访问.
所以在老师给的代码中如果要访问外部类的私有变量a,需要先在内部类中写
SOuter so=new Souter();
public void test() {
System.out.println("访问外部类中私有变量a:" + so.a );
所以在老师给的代码中如果要访问外部类的私有变量a,需要先在内部类中写
SOuter so=new Souter();
public void test() {
System.out.println("访问外部类中私有变量a:" + so.a );
2017-10-13
public static void main(String[] args) {
HelloWorld o = new HelloWorld ();
Inner inn =o.new Inner();
inn.show();
}
HelloWorld o = new HelloWorld ();
Inner inn =o.new Inner();
inn.show();
}
2017-10-11
public class HelloWorld{
private String name = "imooc";
int age = 20;
public class Inner {
String name = "爱慕课";
public void show() {
System.out.println("" + HelloWorld.this.name);
System.out.println("" + name);
System.out.println("" + age);
}
}
}
private String name = "imooc";
int age = 20;
public class Inner {
String name = "爱慕课";
public void show() {
System.out.println("" + HelloWorld.this.name);
System.out.println("" + name);
System.out.println("" + age);
}
}
}
2017-10-11