`

继承--派生类类构造

阅读更多
 class Boy{
	public Boy(){
		System.out.println("Boy");
	}
	public Boy(int i){
		System.out.println("Boy" + i);
	}
}
 
class God extends Boy{
	
	public God(){
		System.out.println("God");
	}
	
	public God(int i){
		//super(i);
		System.out.println("God" + i);
	}
}
public class TestException  {
	
	public static void main(String[] args){
		//God g = new God();
		God p = new God(22);
	}
} 

 输出结果为:

Boy
God22

这表明:一派生类实例化时会先调用基类的无参构造方法,二如果要调用基类的有参构造方法,使用super

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics