/** *基类包含一静态变量、包含一实例变量 *包含一个静态初始化块以及一个构造子 */ class Base{ public staticint a = 10; public int b = 20; static { System.out.println("StaticInit Base " + a); //System.out.println("NullInit " + b); } public Base() { System.out.println("Init Base" + this.b); } } /** *一级子类和基类包含的内容一样 **/ class SuperClass extends Base{ public staticint a1 = getSuperStaticNumber(); public int b1 = getSuperInstanceNumber(); public SuperClass() { System.out.println("InitSuperClass" + this.b1); } static { System.out.println("StaticInit SuperClass" + a1); } public staticint getSuperStaticNumber() { System.out.println("Staticmember init"); return 100; } public int getSuperInstanceNumber() { System.out.println("Instancemember init"); return 200; } } /** *二级子类为测试该代码的驱动类 */ public class SubClass extends SuperClass{ public staticint a2 = getStaticNumber(); public int b2 = getInstanceNumber(); public SubClass() { System.out.println("InitSubClass " + this.b2); } public staticint getStaticNumber() { System.out.println("Staticmember init Sub"); return 1000; } public int getInstanceNumber() { System.out.println("Instancemember init Sub"); return 2000; } public static void main(String args[]) { new SubClass(); } static { System.out.println("StaticInit " + a2); } } 这段代码会有以下输出: Static InitBase 10 Staticmember init Static InitSuperClass 100 Staticmember init Sub Static Init1000 Init Base20 Instancemember init InitSuperClass 200 Instancemember init Sub InitSubClass 2000
感觉好复杂啊。。。
|