ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java继承当中都有一些什么样的构造函数规则?

2021-01-22 18:02:45  阅读:161  来源: 互联网

标签:java win 什么样 System println out super 构造函数


6.继承当中的构造函数规则  

马克-to-win:继承当中的构造函数规则貌似复杂: 记住我给你的以下几条口诀, 你高枕无忧。1)如果你在某类中写了带参构造函数,系统就不会再为你在那类中自动添加无参构造函数了。2)如你没有写无参构造函数,且机器也不会为你自动添加这个无参构造函 数时(因为你已经有带参构造函数了),你不可以主动调无参构造函数。3)子类的构造函数中不能人为的写两个super。4)构造函数中要是你人工想写super,super必须为第一句话。构造函数中要是你不写super,机器会为你加无参数super().马克-to-win:5)既然super必须为第一句话,创建子类对象时,构造函数调用次序为,先最低的超类直到最高的子类。

例1.6.1---

class AAAMark_to_win {
    AAAMark_to_win() {
        System.out.println("Inside AAAMark_to_win's constructor.");
    }
    AAAMark_to_win(int j) {
        System.out.println(j);
    }
}
class BBB extends AAAMark_to_win {
    BBB() {
        super(3);
        System.out.println("Inside BBB's constructor.");
    }
    BBB(int i) {
        System.out.println(i);
    }
}
class C extends BBB {
    C(int a) {
/*马克-to-win: super必须是第一句话,彻底不写也行。因为机器会为你自动加。
super must be the first statement. without it, also ok.the result
is exactly the same as right now, because machine will add automatically for you. but if you have it, it must be the first statement.
*/      
//        super();//注意这上下两句,只能保留一个
        super(1);
        System.out.println("Inside C's constructor.");
    }
}

public class Test {
    public static void main(String args[]) {
/*马克-to-win: 这里你不能写成C(),即使不谈继承,也一样。因为你已经有C(int a) {} ,系统不会为你自动添加C() {} 。
 
here you can not write as new C(); even without inheritance,still, you can not write as new C(); because if you have C(int a) {} already, machine will not automatically add C() {} for you.*/
        C c = new C(5);
    }
}

 

更多内容请见原文,原文转载自:https://blog.csdn.net/qq_44639795/article/details/103117875

标签:java,win,什么样,System,println,out,super,构造函数
来源: https://www.cnblogs.com/shituxingzhe1949/p/14314615.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有