java继承当中都有一些什么样的构造函数规则?
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
最新文章
- StringBuffer(线程安全)StringBuilder(非线程安全)
- C++虚函数、虚继承、对象内存模型(转)
- 创建,增加,删除mysql表分区
- [实战]MVC5+EF6+MySql企业网盘实战(28)——其他列表
- C#下调用C++ SDK的编码常识
- plsql快速选中一行的快捷键
- Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解(四)
- 【iOS之轮播视图、自定义UIPageControl】
- php和java静态变量用途的思考
- silverlight中使用Json读取数据
- linq to entity asp.net mvc 多字段排序
- 获得正在编辑行的数据 esayui datagrid
- Note over Chinese Encodings
- 数据提交成功后如何避免alert被window.location.reload()影响
- 基于QT的中国象棋
- Java版本知识
- linux安装activemq
- Chrome 性能监测
- Virtual DOM的简单实现
- ELMAH--Using HTTP Modules and Handlers to Create Pluggable ASP.NET Components 77 out of 90 rated th
热门文章
- LeetCode-082-删除排序链表中的重复元素 II
- Go redis hash存储结构体
- 震撼的Linux全景图:业界成熟的内核架构长什么样?
- 【pip install】+【环境变量配置】
- laravel7 webuploader上传图片
- Windows 10平台安装PostgreSQL 14.2详细教程
- RabbitMQ Go客户端教程6——RPC
- 74CMS 3.4 反射型XSS漏洞
- 对路径“C:\inetpub\wwwroot\Test\Temper\”的访问被拒绝 【已解决】
- 【模板】动态 DP