子父类中的构造函数
来源:互联网 发布:海量数据去重 编辑:程序博客网 时间:2024/05/16 15:54
在对子类对象进行初始化时候,父类的构造函数也会运行,因为在子类的构造函数第一行有隐式语句super();只要构造函数第一行没有有参数super,第一行都会有super();
以下代码运行结果为
空Demo1
空Demo2
package cn.my.initialize;import org.junit.Test;class Demo1 {public Demo1(){System.out.println("空Demo1");}}class Demo2 extends Demo1{public Demo2(){System.out.println("空Demo2");}}public class Demo{public static void main(String[] args){new Demo2();}}
下面的代码会编译错误,因为在Demo2的构造方法中第一行的有隐式super(),而Demo1中没有空构造函数,所以会编译失败
package cn.my.initialize;import org.junit.Test;class Demo1 {public Demo1(String id){System.out.println("id Demo1");}}class Demo2 extends Demo1{public Demo2() //这里会编译失败{System.out.println("空Demo2");}}public class Demo{public static void main(String[] args){new Demo2();}}以下代码结果为
空Demo1
id Demo2
在Demo2的非空构造函数的第一行有隐式super(),所以会先输出 空Demo1 。
package cn.my.initialize;import org.junit.Test;class Demo1 {public Demo1(String id){System.out.println("id Demo1");}public Demo1(){System.out.println("空Demo1");}}class Demo2 extends Demo1{public Demo2(String id){System.out.println("id Demo2");}}public class Demo{public static void main(String[] args){new Demo2("id");}}
0 0
- 子父类中的构造函数
- 子父类中的构造函数
- 子父类中的构造函数
- 子父类中的构造函数的特点
- 子父类中的构造函数的特点
- C#中的构造函数
- 构造函数中的引用
- DotNet中的构造函数
- C#中的构造函数
- c#中的构造函数
- C#中的构造函数
- 枚举中的构造函数
- PHP中的构造函数
- Delphi中的构造函数
- C#中的构造函数
- Delphi中的构造函数
- 类中的构造函数
- C#中的构造函数
- vector的reserve和resize
- Android手机 Fildder真机抓包
- 求对应输出
- 数据库中use master语句
- 金蝶K3无法创建数据库,请检查目录的错误的解决办法。
- 子父类中的构造函数
- 深入理解Hadoop集群和网络
- Visual Studio 2010 自定义精简安装
- 也谈面向对象
- learn with python-第十五章:对象的集合
- 黑马程序员_面向对象知识点整理
- IOS 序列化与反序列化 NSKeyedUnarchiver
- 编程算法 - 从1到n整数中1出现的次数 代码(C)
- java分页算法