Java 构造函数中的super()
来源:互联网 发布:苹果电脑安装网页软件 编辑:程序博客网 时间:2024/06/01 09:26
public class Test { public static void main(String[] args) { new Son("baidu"); new Son(); } } class Farther{ public Farther(){ System.out.println("I am father"); } public Farther(String name){ System.out.println("I am father:"+name); } } class Son extends Farther{ public Son(){ System.out.println("I am son"); } public Son(String name){ //super("google"); System.out.println("I am son:"+name); } }
Java在实例化一个对象的时候,如果没有显式使用super(),则会先调用父类的无参构造函数(不是和自己构造函数参数数量对应的那个),然后调用子类的构造函数,如果父类不是Object类,则一直向上追溯到Object类为止,super()只能在构造函数的第一行使用,在别的地方使用均为非法,一般情况下构造函数不用写super(),但是如果一个类有多个构造函数的时候,为了便于理解,往往要显式调用super()。
上面代码的运行结果为:
I am fatherI am son:baiduI am fatherI am son
0 0
- Java 构造函数中的super()
- Java 构造函数 super问题
- 关于子类构造函数中的super问题
- super在构造函数中的运用
- 构造函数,super,this
- Java中的构造函数
- Java中的构造函数
- java 中的构造函数
- Java中的构造函数
- java中的构造函数
- java中的构造函数
- java中的构造函数
- java中的构造函数
- JAVA学习,this(),super()调用构造函数研究
- java父类子类构造函数——super()
- JAVA中super和this调用构造函数
- Java - 构造函数、this 和 super的使用
- Java 子类调用父类构造函数----super()详解
- Android---08---布局与单位
- VS2008C#连接SQLserver2008示例
- 微信APP支付接口文档
- 《分布式java运用:基础与实践》笔记2
- 深入理解Java内存模型 volatile
- Java 构造函数中的super()
- RNN-RBM 网络架构及程序解读
- JavaScript 函数定义
- 数据类型与数组运算
- HTTPS协议,SSL协议及完整交互过程
- ASP.NET web.config中的代码
- Sqrt(x)
- VS 2010 调试程序时,遇到错误直接跳出,不崩在错处
- Ubuntu上简单搭建nginx