调用父类构造器:super

import static java.lang.System.*;
class Base{
	public String name;
	public double weight;
	public Base(String name,double weight){
		this.name=name;
		this.weight=weight;
	}
}

public class getSuperConstructor extends Base{
	public int age;
	public getSuperConstructor (String name,double weight,int age){
		//-通过super来调用父类构造器
		//-只能在构造器可执行代码的第一行
		super(name,weight);
		this.age=age;
	}
	
	public static void main(String[] args){
		getSuperConstructor sub=new getSuperConstructor ("张三",75.5,32);
		out.println(sub.name);
		out.println(sub.weight);
		out.println(sub.age);
	}
}

运行结果:

posted on 2017-12-11 13:55  不丶懂  阅读(191)  评论(0编辑  收藏  举报

导航