class Animal
{
//shift+alt+s 选择generate getters and setters
//自动生成属性
private String name;
private String sex;
private int age;
public Animal(String dName, String dSex, int dAge) {
// TODO Auto-generated constructor stub
this.name = dName;
this.sex = dSex;
this.age = dAge;
}
public void eat()
{
System.out.println("Animal eat");
}
public void sleep()
{
System.out.println("Animal sleep");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
class dog extends Animal //继承父类
{
public dog(String dName, String dSex, int dAge) {
/*
* 父类没有实现无参构造函数,所以使用super来调用父类无参构造函数
* */
super(dName, dSex, dAge);
}
}
public class HttpFunc {
public static void main(String[] args) {
dog poke = new dog("poke", "M", 12);
System.out.println(poke.getName());
}
}
java类继承案例
最新推荐文章于 2024-09-13 13:13:04 发布