编写动物世界的继承关系代码,动物(Animal)包括山羊(Goat)和狼(Wolf),它们吃(eat)的行为不同:山羊吃草,狼吃肉,但走路(walk)的行为是一致的。通过继承实现以上需求,并编写AnimalTest测试类进行测试。
public class Animal{
public void eat(){}
public void walk(){}
}
public class Goat extend Animal{
public void eat(){
System.out.print("Goat eat grass");
}
public void walk(){}
}
```java
//测试
public class AnimalTest{
public static void main(String[] args) {
Animal goat=new Goat();
goat.eat();
}
}
```
class Animal {
public void eat() {
}
public void walk() {
System.out.println("行为:走路");
}
}
class Goat extends Animal {
@Override
public void eat() {
System.out.print("Goat eat grass");
}
}
class Wolf extends Animal {
@Override
public void eat() {
System.out.println("Wolf eat meat");
}
}
class AnimalTest {
public static void main(String[] args) {
Animal goat = new Goat();
goat.eat();
Animal wolf = new Wolf();
wolf.eat();
wolf.walk();
}
}