子类对父类构造方法调用小结

1、若是父类中没有构造函数,即便用默认的构造函数,那子类的构造函数会自动调用父类的构造函数 
Java代码   收藏代码
  1. class Father {      
  2.   private int a, b;      
  3.      
  4.   void show() {      
  5.     System.out.println(a);      
  6.   }      
  7. }      
  8.      
  9. class Son extends Father {      
  10.   private int c, d;      
  11.      
  12.   Son(int c, int d) {      
  13.     this.c = c;      
  14.     this.d = d;      
  15.   }      
  16. }      
  17.      
  18. public class ConstructionTest {      
  19.   public static void main(String args[]) {      
  20.     Son s = new Son(23);      
  21.     s.show();      
  22.   }      
  23. }   

输出结果0,说明子类的构造函数自动调用父类的无参构造函数,初始化父类的成员为0 

2、若是父类中定义了无参构造函数,子类的构造函数会自动调用父类的构造函数 

Java代码   收藏代码
  1. class Father {      
  2.   private int a, b;      
  3.      
  4.   Father() {      
  5.     System.out.println("father done");      
  6.   }      
  7.      
  8.   void show() {      
  9.     System.out.println(a);      
  10.   }      
  11. }      
  12.      
  13. class Son extends Father {      
  14.   private int c, d;      
  15.      
  16.   Son(int c, int d) {      
  17.     this.c = c;      
  18.     this.d = d;      
  19.   }      
  20. }      
  21.      
  22. public class ConstructionTest {      
  23.   public static void main(String args[]) {      
  24.     Son s = new Son(23);      
  25.     s.show();      
  26.   }      
  27. }   
  28. 输出结果:father done    
  29.   
  30. 0  
  31. 说明重写了默认的无参构造函数,子类自动调用这个函数,父类的成员仍是被初始化为0.  
  32.   
  33. 3、 若是定义了有参构造函数,则不会有默认无参构造函数,这样的话子类在调用父类的无参构造函数的时候会出错(没有用super调用父类有参构造函数的状况下)  
  34.   
  35. class Father {      
  36.   private int a, b;      
  37.      
  38.   Father(int a, int b) {      
  39.     this.a = a;      
  40.     this.b = b;      
  41.   }      
  42.      
  43.   void show() {      
  44.     System.out.println(a);      
  45.   }      
  46. }      
  47.      
  48. class Son extends Father {      
  49.   private int c, d;      
  50.      
  51.   Son(int c, int d) {      
  52.     this.c = c;      
  53.     this.d = d;      
  54.   }      
  55. }      
  56.      
  57. public class ConstructionTest {      
  58.   public static void main(String args[]) {      
  59.     Son s = new Son(23);      
  60.     s.show();      
  61.   }      
  62. }   
  63. 输出结果:  
  64. Exception in thread "main" java.lang.NoSuchMethodError: Father: method <init>()V  
  65. not found  
  66. at Son. <init>(Son.java:5)  
  67. at ConstructionTest.main(ConstructionTest.java:6)  


     实际上,在子类的每个构造函数中,都会调用父类的构造函数。调用哪个构造函数,这里分两种状况: 
    (1)不显式的用super来指定。这种状况下,会默认的调用无参数的构造函数。若是父类中没有无参数的构造函数,那么程序就会出错,就像以上那个例子。把Father类中的那个构造函数去掉,程序就能经过编译。若是一个类中,没有一个构造函数,那么编译器就会给这个类加上一个无参构造函数。 
  (2)显式的使用super来指定调用哪个构造函数。在Son类的构造函数中加上super(c,d),程序也能够经过编译。 


另外,看以下例子: 
Java代码   收藏代码
  1. A.java  
  2.   
  3. class A{  
  4.   
  5. public A(){  
  6.   
  7. System.out.println("A无参数构造函数被调用");  
  8.   
  9. }  
  10.   
  11. public A(int i){  
  12.   
  13. System.out.println("A有参数构造函数被调用");  
  14.   
  15. }  
  16.   
  17. }  
  18.   
  19. B.java  
  20.   
  21. class B extends A{  
  22.   
  23. public B(){  
  24.   
  25. }  
  26.   
  27. public B(int i){  
  28.   
  29. }  
  30.   
  31. }  
  32.   
  33. public static void main(String[] args){  
  34.   
  35. new B(1);  
  36.   
  37. }  
  38.   
  39. }  

  这个程序打印:无参数的构造函数被打印。而不是:有参数的构造函数被打印。这说明你只要不使用super来指定,他就会默认的去调用父类无参数的构造函数。 
相关文章
相关标签/搜索