Java

2019-03-07 Java super

2019-03-07  本文已影响0人  yangsunior

super

super实例代码分析

1. 子类默认使用super() 调用父类构造器

public class AJavaTest{
      public static void main(String args[]) {
            new AJavaTestZi();
      }
}
class AJavaTestFu {
      int num;
      
      AJavaTestFu() {
            num = 10;
            System.out.println("Fu 001: " + num);
      }
      
      AJavaTestFu(int num) {
            System.out.println("Fu 002: " + num);
      }
}
class AJavaTestZi extends AJavaTestFu {
      int num;
      AJavaTestZi()
      {
//          super();//调用的就是父类中的空参数的构造函数。
            
            System.out.println("AJavaTestZi 001");
      }
}

OUTPUT:

Fu 001: 10
AJavaTestZi 001

2. this()

public class AJavaTest{
      public static void main(String args[]) {
            new AJavaTestZi(25);
      }
}
class AJavaTestFu {
      int num;
      
      AJavaTestFu() {
            num = 10;
            System.out.println("Fu 001: " + num);
      }
      
      AJavaTestFu(int num) {
            System.out.println("Fu 002: " + num);
      }
}
class AJavaTestZi extends AJavaTestFu {
      int num;
      AJavaTestZi()
      {
//          super();//调用的就是父类中的空参数的构造函数。
            
            System.out.println("AJavaTestZi 001");
      }
      
      AJavaTestZi(int x)
      {
            this();
            System.out.println("AJavaTestZi 002: "+x);
      }
}

OUTPUT:

Fu 001: 10
AJavaTestZi 001
AJavaTestZi 002: 25

3. 将this()改为super()

public class AJavaTest{
      public static void main(String args[]) {
            new AJavaTestZi(25);
      }
}
class AJavaTestFu {
      int num;
      
      AJavaTestFu() {
            num = 10;
            System.out.println("Fu 001: " + num);
      }
      
      AJavaTestFu(int num) {
            System.out.println("Fu 002: " + num);
      }
}
class AJavaTestZi extends AJavaTestFu {
      int num;
      AJavaTestZi()
      {
//          super();//调用的就是父类中的空参数的构造函数。
            
            System.out.println("AJavaTestZi 001");
      }
      
      AJavaTestZi(int x)
      {
            super();
            System.out.println("AJavaTestZi 002: "+x);
      }

    等同于
      AJavaTestZi(int x)
      {
            System.out.println("AJavaTestZi 002: "+x);
      }
}

OUTPUT:

Fu 001: 10
AJavaTestZi 002: 25

4. 将super()改为super(30)

public class AJavaTest{
      public static void main(String args[]) {
            new AJavaTestZi(25);
      }
}
class AJavaTestFu {
      int num;
      
      AJavaTestFu() {
            num = 10;
            System.out.println("Fu 001: " + num);
      }
      
      AJavaTestFu(int num) {
            System.out.println("Fu 002: " + num);
      }
}
class AJavaTestZi extends AJavaTestFu {
      int num;
      AJavaTestZi()
      {
//          super();//调用的就是父类中的空参数的构造函数。
            
            System.out.println("AJavaTestZi 001");
      }
      
      AJavaTestZi(int x)
      {
            super(30);
            System.out.println("AJavaTestZi 002: "+x);
      }
}

OUTPUT:

Fu 002: 30
AJavaTestZi 002: 25

父类无参构造器不会再被调用。

上一篇下一篇

猜你喜欢

热点阅读