java练习 继承02
程序员文章站
2022-07-15 18:40:30
...
请编码实现动物世界的继承关系:
动物(Animal)具有行为:吃(eat)、睡觉(sleep)、移动(move)
动物包括:兔子(Rabbit),老虎(Tiger),鹰(eagle)
这些动物吃、移动的行为各不相同(eat,move动作不同);但睡觉的行为是一致的。
public class Animal {
public void eat() {
System.out.println("eating...");
}
public void sleep() {
System.out.println("sleeping...");
}
public void move() {
System.out.println("moving...");
}
}
public class Rabbit extends Animal{
public void eat() {
System.out.println("Rabbit eating...");
}
public void move() {
System.out.println("Rabbit moving...");
}
}
public class Tiger extends Animal{
public void eat() {
System.out.println("Tiger eating...");
}
public void move() {
System.out.println("Tiger moving...");
}
}
public class Eagle extends Animal{
public void eat() {
System.out.println("Eagle eating...");
}
public void move() {
System.out.println("Eagle moving...");
}
}
上一篇: 针对接口编程,而不是针对实现编程
下一篇: Hadoop单机环境搭建