Java 子类初始化过程

//父类
class Animal{
	private String name;
	private int age;
	Animal(String name, int age) {//创建父类构造器
		super();
		this.name = name;
		this.age = age;
	}
	public String setName(){//传递参数
		return name;
	}
	public int  setAge(){//传递参数
		return age;
	}
}
//子类
class Fish extends Animal{
	public String color;
	Fish(String name, int age,String color) {//创建父类构造器
		super(name, age);//调用父类的构造器
		this.color=color;
	}
}
//测试类
public class myanimal {
	public static void main(String[] args) {
		Fish f=new Fish("尼莫",18,"橘黄色");
		System.out.println(f.setName()+"\n"+f.setAge()+"\n"+f.color);
	}
}

 

posted @ 2017-03-26 22:06  *小嘻嘻*  阅读(1109)  评论(0编辑  收藏  举报