class A{ public String context=test(); public static int num=getNum(); { System.out.print("A3"); } static{ System.out.print("A1,"); } public A(){ System.out.print("A2,"); } public String test() { String str="A.test(),"; System.out.print(str); return str; } public static int getNum()//它只属于A { System.out.print("A.getNum(),"); return 11; } } class B extends A{ { System.out.print("c"); } static{ System.out.print("a,"); } public B(){ System.out.print("b,"); } public String test()//覆盖 { String str="B.test(),"; System.out.print(str); return str; } public static int getNum()//它只属于B,不能覆盖 { System.out.print("B.getNum(),"); return 21; } } public class InitTest{ public static void main(String[] ars){ new B(); //执行到此处,结果: A.getNum(),A1,a,B.test(),A3A2,cb, [Page] System.out.println(); new B(); //执行到此处,结果: B.test(),A3A2,cb, } }
(责任编辑:admin) |