JAVA之内部类面试题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/z249486888/article/details/83246056

要求:分别输出6、5、4

public class test{
	public static void  main(String [] args) {
		Father.Son son = new Father().new Son();
		son.a();
	}
}
class Father{
	int a=4;
	class Son{
		int a =5;
		public void a() {
			int a = 6;
		System.out.println(?); //1
		System.out.println(?); //2
		System.out.println(?); //3
		}
	}
}

1、a

2、this.a

3、new Father().a   通过匿名对象调用a

 或  Father.this.a     通过外部名限定this对象,Father.this 代表Father的this  即Father的对象

注意!外部类和内部类没有继承关系

猜你喜欢

转载自blog.csdn.net/z249486888/article/details/83246056