标签:style c class blog code java
SUPER可调用父类的构造方法,但要注意默认调用和参数调用。
同时,在继承类时,可以用SUPER调用其它非构造方法哟。
class Test extends Object{ public Test(){ System.out.println("Test1 First thing"); } public Test(String welcome){ System.out.println("Test1 " + welcome); } protected void doSomething(){ System.out.println("Test1 doSomething"); } protected Test doit(){ return new Test(); } } class Test2 extends Test{ public Test2(){ super("welcome string"); super.doSomething(); } public Test2(String welcom){ //super(); super("welcome string"); super.doSomething(); System.out.println("Test2 " + welcom); } public void doSomethingnew(){ System.out.println("Test2 doSomethingnew"); } public void doSomething(){ System.out.println("Test2 doSomething"); } protected Test2 doit(){ return new Test2(); } } class HelloJava{ public static void main(String[] args){ Test test1 = new Test(); Test2 test2 = new Test2("hello"); test1.doSomething(); test2.doSomething(); test2.doSomethingnew(); } }
JAVA构造方法,继承关系和SUPER关键字,布布扣,bubuko.com
标签:style c class blog code java
原文地址:http://www.cnblogs.com/aguncn/p/3752459.html