java继承
package com.wt010.myextends; /** * @author konecms * @date 2018年1月9日 上午10:44:06 * */ public class Shape { public int i=100; public String s="shape string"; { System.out.println("shape block"); } public Shape( ) { System.out.println("Shap Constructor ."); } Draw a=new Draw("shape"); public void printType() { System.out.println("type:shape"); } public static void printName() { System.out.println("name:shape."); } protected void dosome() { System.out.println("shape private"); } }
package com.wt010.myextends; /** * @author konecms * @date 2018年1月9日 上午11:16:56 * */ public class Circle extends Shape { public int i=200; public static String s="circle string"; static { System.out.println("circle static block ."); } Draw a=new Draw("circle"); public Circle() { System.out.println("Circle Constructor"); } public void dosome() { } public void printType() { System.out.println("type:circle"); } public static void printName() { System.out.println("name:circle"); } }
package com.wt010.myextends; /** * @author konecms * @date 2018年1月9日 上午11:50:59 * */ public class Draw { public Draw(String type) { System.out.println(type+" Draw Constructor ."); } }
package com.wt010.myextends; /** * @author konecms * @date 2018年1月9日 上午11:20:39 * 关于继承: * */ public class MyExtendsDEMO { public static void main(String[] args) { // TODO Auto-generated method stub Shape a=new Circle(); //Shape b=new Circle(); //int i=a.i; //System.out.println(i); //a.printType(); //Circle.printName(); } }
结果:
circle static block .
shape block
shape Draw Constructor .
Shap Constructor .
circle Draw Constructor .
Circle Constructor