首页 文章 类的继承

类的继承

2021-08-07 11:49  浏览数:545  来源:username    

public class Animal { private String name; private int month; private String species;
public Animal() {} public String getName() { return name; }
public void setName(String name) {
this.name = name;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public String getSpecies() {
return species;
}
public void setSpecies(String species) {
this.species = species;
}
public void eat(){
System.out.println(this.getName()+"在吃东西");
}
}
public class Cat extends Animal {
private double weight;
public Cat() {
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public void run(){
System.out.println(this.getName()+"是一只"+getSpecies()+"的猫,他在跑。");
}
}
public class Dog extends Animal {
private String sex;
public Dog() {
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public void sleep(){
System.out.println(this.getName()+"现在"+this.getMonth()+"个月大,在睡觉。");
}
}
public class Test {
public static void main(String[] args) {
Cat one=new Cat();
one.setName("十二");
one.setSpecies("中华田园猫");
one.eat();
one.run();
System.out.println("=================");
Dog two=new Dog();
two.setName("负十二");
two.setMonth(2);
two.eat();
two.sleep();
}
}



声明:以上文章均为用户自行添加,仅供打字交流使用,不代表本站观点,本站不承担任何法律责任,特此声明!如果有侵犯到您的权利,请及时联系我们删除。

字符:    改为:
去打字就可以设置个性皮肤啦!(O ^ ~ ^ O)