博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
父类如果不写无参构造方法,子类会报错
阅读量:6924 次
发布时间:2019-06-27

本文共 2564 字,大约阅读时间需要 8 分钟。

1. 如果在类中你提供了其他有参的构造器,则编译器不会提供默认的无参构造器。

 

class Animal {	Animal(String name) { }	public static void main(String[] args){		Animal a = new Animal();	}}

 

 

该段代码编译不会通过,报错信息如下:

 

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 	The constructor Animal() is undefined

 

 

2. 如果在类中你没有提供任何构造器,则编译器会提供一个默认的无参构造器。

 

class Animal {	public static void main(String[] args){		Animal a = new Animal();	}}

该段代码则会顺利编译通过。

 

 

3. 如果你提供了一个构造器,你无须手动添加super()到你的构造器,编译器会默认添加。

 

class Animal {	Animal (){		System.out.println("----Animal无参构造器-----");	}}class Horse extends Animal {	Horse() {//		super(); //此行代码有与没有都是一样的		System.out.println("----Horse无参构造器-----");	}	Horse(int a) {//		super(); //此行代码有与没有都是一样的		System.out.println("----Horse有参构造器-----");	}	public static void main(String[] args){		Horse a = new Horse();		Horse b = new Horse(3);	}}

打印信息如下:

 

 

----Animal无参构造器---------Horse无参构造器---------Animal无参构造器---------Horse有参构造器-----

4. 如果父类未提供无参构造器,子类会报错:

 

 

class Animal {	Animal (int a){		System.out.println("----Animal有参构造器-----");	}}class Horse extends Animal {	Horse() {		System.out.println("----Horse无参构造器-----");	}	public static void main(String[] args){		Horse a = new Horse();	}}

该代码不会编译通过,报错如下:

 

 

Implicit super constructor Animal() is undefined. Must explicitly invoke another constructor

5. 如果构造器中添加了this引用该类的其他构造器,或者添加了super()调用父类构造器,this和super必须在构造器第一行,this引用其他构造器和super()语句不会同时出现

 

 

class Animal {	Animal (){		System.out.println("----Animal无参构造器-----");	}}class Horse extends Animal {	String name;	Horse() {		System.out.println("----Horse无参构造器-----");		this("马");  //提示错误,	}	Horse(String name){		this.name = name;	}}

具体错误提示为:Constructor call must be the first statement in a constructor

 

 

6. 如果构造器中添加了this引用该类的其他构造器,或者添加了super()调用父类构造器,如果this和super中有参数,则只能是静态的方法和静态变量或常量

 

class Animal {	Animal (){		System.out.println("----Animal无参构造器-----");	}}class Horse extends Animal {	String name;	Horse() {		this(getRondomName()); //代码段A		System.out.println("----Horse无参构造器-----");	}	Horse(String name){		System.out.println("----Horse有参构造器-----"); //代码段B		this.name = name;	}	static String getRondomName(){		int x = (int) (Math.random() * 5);		String name = new String[] {"马", "猪", "牛", "羊","猫"}[x];		return name;	}	public static void main(String[] args){		Horse a = new Horse();	}}

打印结果如下:

 

 

----Animal无参构造器---------Horse有参构造器---------Horse无参构造器-----

如果调用了this引用其他构造器,那么super()将不会在该构造器中调用,所以super()将在代码段B前面一行被调用,而不是在代码段A前面一行。

 

如果把getRondomName方法前的static去掉,则会提示错误:Cannot refer to an instance method while explicitly invoking a constructor

转载于:https://www.cnblogs.com/zbdxcyg/p/7479804.html

你可能感兴趣的文章
三亚旅游攻略-自由人实用指南
查看>>
我的友情链接
查看>>
Kotlin语言使用反射机制编写运行时View注入
查看>>
vba获取最后一行一列,复制粘贴特定一列的值
查看>>
精美案例展示:立体动感的视差滚动效果网站作品!
查看>>
我的友情链接
查看>>
Netty 100万级高并发服务器配置
查看>>
我的友情链接
查看>>
Jmeter如何实现参数化用户,并且管理Cookie
查看>>
spring依赖注入IOC原理
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
IntelliJ IDEA15优化设置
查看>>
用 mapreduce 怎么处理数据倾斜问题?
查看>>
maven+eclipse+tomcat配置
查看>>
STL容器,外加一些小细节;我是个小菜鸡
查看>>
[转载] 中华典故故事(孙刚)——40 不见棺材不落泪,不到黄河不死心
查看>>
extjs4 设置第一个datefield值小于第二个
查看>>
算法导论第四章
查看>>
拆笔记本
查看>>