java关于继承的代码_java编写动物世界的继承关系代码
满意答案bjtm0210推荐于 2017.12.16采纳率:57%等级:8已帮助:611人我写了一个,内容比较简单的。代码如下:public class AnimalTest {Animal animal;public void eat(Animal animal){animal.eat();}public void walk(Animal animal){animal.walk();}p...
满意答案
bjtm0210
推荐于 2017.12.16
采纳率:57% 等级:8
已帮助:611人
我写了一个,内容比较简单的。代码如下:public class AnimalTest {
Animal animal;
public void eat(Animal animal){
animal.eat();
}
public void walk(Animal animal){
animal.walk();
}
public static void main(String args[]){
Animal animal=new Animal("animal");
Wolf w=new Wolf("wolf");
Goat g=new Goat("goat");
AnimalTest at=new AnimalTest();
at.eat(animal);
at.eat(w);
at.eat(g);
at.walk(animal);
at.walk(w);
at.walk(g);
}
}
class Animal {
String name;
public Animal(String name){
this.name=name;
}
public Animal(){}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void eat(){
System.out.println("animal eat");
}
public void walk(){
System.out.println("animal walk");
}
public String toString(){
return name;
}
}class Wolf extends Animal {
public Wolf(String name){
super(name);
}
public Wolf(){}
public void eat(){
System.out.println("wolf eat meat");
}
public void walk(){
System.out.println("wolf walk");
}
public String toString(){
return name;
}
}class Goat extends Animal {
public Goat(String name){
super(name);
}
public Goat(){}
public void eat(){
System.out.println("goat eat grass");
}
public void walk(){
System.out.println("goat walk");
}
public String toString(){
return name;
}
}
04分享举报
更多推荐
所有评论(0)