`

JAVA Inner Class

    博客分类:
  • JAVA
阅读更多

public class TestInnerClass {


	void m(){
		System.out.println("Outer.m()"+"-"+this);
	}
	
	public class Inner extends TestInnerClass{
		void m(){
			TestInnerClass.this.m();
			System.out.println("Inner.m()" + "-" +this);
		}
	}
	
	public static void main(String[] args) {

		TestInnerClass out = new TestInnerClass();
		TestInnerClass.Inner i1 = out.new Inner();
		Inner i2 = i1.new Inner();
		
		System.out.println(out);
		System.out.println(i1);
		System.out.println(i2);
		
		System.out.println("=================");
		out.m();
		System.out.println("=================");
		i1.m();
		System.out.println("==================");
		i2.m();
	}

}

output:
TestInnerClass@3ce53108
TestInnerClass$Inner@6af62373
TestInnerClass$Inner@459189e1
=================
Outer.m()-TestInnerClass@3ce53108
=================
Outer.m()-TestInnerClass@3ce53108
Inner.m()-TestInnerClass$Inner@6af62373
==================
Outer.m()-TestInnerClass@3ce53108
Inner.m()-TestInnerClass$Inner@6af62373
Inner.m()-TestInnerClass$Inner@459189e1

尽管逻辑上看可以嵌套任意多层次的类,但是编译后实现只存在两个类 TestInnerClass 和TestInnerClass.Inner
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics