标签:style color java ar strong sp on c 代码
内部类:简单体现:
import out.in; class out { private int num = 3; class in { public void showou() { System.out.println(num); } } public void showin() { in BLF = new in(); BLF.showou(); } } public class Main { public static void main(String[] args) { out BLF = new out(); BLF.showin(); } }
class out { private static int num = 3; static class in { public void showou() { System.out.println(num);//静态类中访问外部类的成员变量也必须是静态的 } public static void showsta() { System.out.println("sta show"+num); } } public void showin() { in BLF = new in(); BLF.showou(); } } public class Main { public static void main(String[] args) { //内部类是静态的,成员是非静态的 out.in BLF = new out.in(); BLF.showou(); //如果内部类是静态的,成员也是静态的情况 out.in.showsta(); } }
class out { int num = 3; class in { int num = 4; public void showou() { int num = 5; System.out.println(num);//如果想打印4,this.num System.out.println(this.num); //也可以 System.out.println(in.this.num); //如果想打印3 System.out.println(out.this.num); } } public void showin() { in BLF = new in(); BLF.showou(); } } public class Main { public static void main(String[] args) { out.in BLF = new out().new in(); BLF.showou(); } }
class out { int num = 3; void show1() { class in { void show() { System.out.print(num); } } in blf = new in();//创建对象,但是不能在外部类中创建 blf.show(); } } public class Main { public static void main(String[] args) { out blfOut = new out(); blfOut.show1(); } }
class out { int num = 3; void show1() { final int x = 5; class in { void show() { System.out.println(x); } } in blf = new in(); blf.show(); } } public class Main { public static void main(String[] args) { out blfOut = new out(); blfOut.show1(); } }
class out { int num = 3; Object show1()//如果这个方法是一个返回对象的方法 { int x = 5; class in { void show() { System.out.println(x); } } Object blf = new in(); //blf.show(); return blf; } } public class Main { public static void main(String[] args) { out blfOut = new out(); Object blfObject = blfOut.show1();//blfObject接受这个对象,那么blfOut的show1方法就执行完了 //那么就意味着show1的x就没了,所以必须加final,final修饰的x是终身为5,不会变化的 } }
标签:style color java ar strong sp on c 代码
原文地址:http://blog.csdn.net/wjw0130/article/details/39510999