A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Michael_xpd 中级黑马   /  2013-10-10 18:12  /  1540 人查看  /  9 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 Michael_xpd 于 2013-10-11 07:01 编辑

“子类中至少会有一个构造函数会访问父类中的构造函数”这句话怎么理解???

9 个回复

倒序浏览
当你不给子类写特定的构造函数的时候,子类会默认调用父类的构造函数
回复 使用道具 举报
{:soso_e126:}我刚才打了好几百字的内容怎么木有了!!!!!!!{:soso_e115:}

评分

参与人数 1黑马币 +5 收起 理由
乔兵 + 5 哎...

查看全部评分

回复 使用道具 举报
在JAVA中,我们通常把函数叫做方法。对于子类调用父类的构造方法可以做出如下解释: 子类无条件地继承父类的不含参数的构造方法。如果子类自己没有构造方法,则它将继承父类的无参数构造方法作为自己的构造方法;如果子类自己定义了构造方法,则在创建新对象时,它将先执行继承自父类的无参数构造方法,然后再执行自己的构造方法。对于父类含参数的构造方法,子类可以通过在自己的构造方法中使用 super 关键字来调用它,但这个调用语句必须是子类构造方法的第一个可执行语句。

评分

参与人数 1技术分 +1 收起 理由
乔兵 + 1

查看全部评分

回复 使用道具 举报
比如你定义
class father{}

class son entends father{
}
每个类都有一个默认的构造函数.son类没有写构造函数,就有个默认构造函数.
假如子类 son定义了 son(){}.   那么在这个构造函数内,就会自动添加进super();就是父类的默认构造函数.
回复 使用道具 举报
子类中至少会有一个构造函数会访问父类中的构造函数,也就是说父类的初始化优先与子类调用,这是因为子类在继承父类时子类要知道父类对其属性的初始化方式。因此,假设自己定义的任何一个没有构造函数的类,该类初始化时,会在子类中的构造函数的第一行默认通过super()语句调用object的构造函数,但是由于有构造函数重载的存在,对于有多个构造函数的子类,有些构造函数可以在第一行同过this()关键字调用该类的其它构造函数,这时这些构造函数就可以说是没有访问父类中的构造函数,或者说是没有直接访问父类的构造函数。这是我的理解,希望对你有帮助。
回复 使用道具 举报
子类的构造函数中默认有个super()方法
回复 使用道具 举报
这是因为,子类在不定义构造函数的时候,默认都会有一个无参数的构造函数,而构造函数的第一句,有个隐式语句,super(),会调用父类中的构造函数,对子类进行初始化。所以,子类至少有一个构造函数会访问父类的构造函数。
如:
class Fu{
}
class Zi extends Fu{
   /*
      Zi(){
          //super();隐式语句
       }
    此处注释代表默认的构造函数
   */
}
回复 使用道具 举报
其实你可以看我写的代码,运行一下看看,就知道怎么回事了,子类的构造函数可以调用自己其他的构造函数,父类也是如此,子类的不同构造函数可以调用父类的不同构造函数,不一定子类child()就一定是super(),可以是调用父类有参数的构造函数,总之在每次实例化时,子类调用父类的构造函数只能调用一次,不能出现多次调用。
  1. package heima.shiba;

  2. public class ExtendsDemo {

  3.         public static void main(String[] args) {
  4.                 child c = new child("小明");
  5.                 child c1 = new child();
  6.                 father f = new father("大明");
  7.                 father f1 = new father();
  8.         }

  9. }
  10. class father {
  11.         public father(){
  12.                 System.out.println("father constructor()");
  13.         }
  14.         public father(String name){
  15.                 this();
  16.                 System.out.println("father constructor(String name)"+name);
  17.         }
  18. }
  19. class child extends father{
  20.         public child(){
  21.                 super("大明");
  22.                 System.out.println("child constructor()");
  23.         }
  24.         public child(String name){
  25.                 this();
  26.                 System.out.println("child constructor(String name)"+name);
  27.         }
  28. }
复制代码
注意:this和super不能同时出现在一个构造函数内,还有不能出现自己调用自己的情况,如果尝试编译器会提示出现循环调用,无法正常编译,
打印结果如下:
//child c = new child("小明");
father constructor()
father constructor(String name)大明
child constructor()
child constructor(String name)小明
//child c1 = new child();
father constructor()
father constructor(String name)大明
child constructor()
//father f = new father("大明");
father constructor()
father constructor(String name)大明
//father f = new father();
father constructor()
如果代码改成这样,
  1. class child extends father{
  2.         public child(){
  3.                 super("大明");
  4.                 System.out.println("child constructor()");
  5.         }
  6.         public child(String name){
  7.                 super();
  8.                 System.out.println("child constructor(String name)"+name);
  9.         }
  10. }
复制代码
结果就是这样了:
father constructor()
child constructor(String name)小明
father constructor()
father constructor(String name)大明
child constructor()
father constructor()
father constructor(String name)大明
father constructor()
是不是明白了,希望能帮到你,实际没有什么实用性,只是纯粹研究罢了
回复 使用道具 举报
张远 中级黑马 2013-10-10 23:27:52
10#
子类构造函数内不出现this调用本类其他构造函数时,默认有super()来调用父类构造函数。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马