日期:2014-05-20 浏览次数:20746 次
public class FatherClild { public static void main(String[] are) { Child c = new Child(); Parent p = c; System.out.println(p.ParentVar); System.out.println(p.ParentStaticVar); System.out.println(p.ParentStaticStr); System.out.println(c.ParentVar); System.out.println(c.ParentStaticVar); System.out.println(c.ParentStaticStr); p.parentMethod(); p.parentStaticMethod(); c.parentMethod(); c.parentStaticMethod(); } } class Child extends Parent { public int ParentVar = 15; public static int ParentStaticVar = 14; //public static String ParentStaticStr = "child"; static { System.out.println("this is a Child static block"); } public Child() { System.out.println("this is a Child construct"); } public void parentMethod() { System.out.println("this is a Child method"); } public int getParentStaticVar() { return super.ParentVar; } public static void parentStaticMethod() { System.out.println("this is a Child static method"); } } /* * */ class Parent { public int ParentVar = 5; public static int ParentStaticVar = 4; public static String ParentStaticStr = "parent"; static { System.out.println("this is a parent static block"); } public Parent() { System.out.println("this is a parent construct"); } public void parentMethod() { System.out.println("this is a parent method"); } public static void parentStaticMethod() { System.out.println("this is a parent static method"); } }