<转载学习>子类对父类构造方法调用小结

本文探讨了Java中子类构造函数如何调用父类构造函数的规则,包括默认调用无参构造函数的情况,以及当父类仅有带参构造函数时如何正确调用。
一、如果父类中没有构造函数,即使用默认的构造函数,那子类的构造函数会自动调用父类的构造函数 
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 

二、如果父类中定义了无参构造函数,子类的构造函数会自动调用父类的构造函数 

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. 三、 如果定义了有参构造函数,则不会有默认无参构造函数,这样的话子类在调用父类的无参构造函数的时候会出错(没有用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来指定,他就会默认的去调用父类无参数的构造函数。 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值