实验内容及要求:实现一个仿真农场,农场包括猫、狗、猪、马等动物。下面给出一个哺乳动物的 类,要求编写包括猫、狗、猪、马等动物的的类,并实现以下要求: 其中狗属哺乳动物,且它的属性有品种之分(在哺乳类基础上增加品种数属性),叫声区别于其他动 物(输出“Woof!”)。令猫、马、猪也属于哺乳动物,其叫声分别为:“Meow!”,“Winnie!”,“Oink!”。编 程分别使各个动物表现为不一样的行为。 定义一个接口 Pet,猫和狗类需要实现 Pet 的所有方法。要求 Pet 包含如会摇尾巴(增加方法,输出 “Tail wagging…”),乞讨食物(增加方法,输出“begging for food…”)等宠物行为。 哺乳动物类如下所示: public class Mammal{ protected int age = 2; protected int weight = 5; public Mammal(){ System.out.println(“Mammal constructor”); } int getAge(){ return age; } void setAge(int a){ age = a; } int getWeight(){ Acts(); class i class Nose(); extends i() 5 class Of76(); interface i(x) 7 class Clowns(); implements i[x] 7 public class Picasso(); Acts i.iMethod(x) public int iMethod(); Nose i(x).iMethod[] public int iMethod{} Of76 i[x].iMethod() public int iMethod(){ Clowns i[x].iMethod[] public int iMethod(){} Picasso Of76[] i = new Nose[3]; Of76[3] i; Nose[] i = new Nose(); Nose[] i = new Nose[3]; return weight; } void setWeight(int w){ weight = w; } void move(){ System.out.println(“Mammal move one step”); } void speak(){ System.out.println(“Mammal speak!”); } void sleep(){ System.out.println(“shhh, I’m sleeping.”); } }
时间: 2024-03-03 11:53:40 浏览: 75
下面是猫、狗、猪、马类的实现,以及Pet接口的定义和实现:
```
public interface Pet {
void wagTail();
void begFood();
}
public class Cat extends Mammal {
public Cat() {
System.out.println("Cat constructor");
}
void speak() {
System.out.println("Meow!");
}
@Override
public void move() {
System.out.println("Cat move one step");
}
@Override
public void sleep() {
System.out.println("Purr, I'm sleeping.");
}
public void chaseMouse() {
System.out.println("Cat is chasing the mouse.");
}
public void climb() {
System.out.println("Cat is climbing the tree.");
}
public void wagTail() {
System.out.println("Tail wagging...");
}
public void begFood() {
System.out.println("Begging for food...");
}
}
public class Dog extends Mammal implements Pet {
private String breed;
public Dog(String breed) {
System.out.println("Dog constructor");
this.breed = breed;
}
void speak() {
System.out.println("Woof!");
}
@Override
public void move() {
System.out.println("Dog move one step");
}
@Override
public void sleep() {
System.out.println("Snore, I'm sleeping.");
}
public void fetch() {
System.out.println("Dog is fetching the ball.");
}
public void sit() {
System.out.println("Dog is sitting.");
}
public void wagTail() {
System.out.println("Tail wagging...");
}
public void begFood() {
System.out.println("Begging for food...");
}
}
public class Pig extends Mammal {
public Pig() {
System.out.println("Pig constructor");
}
void speak() {
System.out.println("Oink!");
}
@Override
public void move() {
System.out.println("Pig move one step");
}
@Override
public void sleep() {
System.out.println("Snort, I'm sleeping.");
}
public void rollInMud() {
System.out.println("Pig is rolling in the mud.");
}
public void eat() {
System.out.println("Pig is eating.");
}
public void wagTail() {
System.out.println("Tail wagging...");
}
public void begFood() {
System.out.println("Begging for food...");
}
}
public class Horse extends Mammal {
public Horse() {
System.out.println("Horse constructor");
}
void speak() {
System.out.println("Winnie!");
}
@Override
public void move() {
System.out.println("Horse move one step");
}
@Override
public void sleep() {
System.out.println("Neigh, I'm sleeping.");
}
public void gallop() {
System.out.println("Horse is galloping.");
}
public void jump() {
System.out.println("Horse is jumping.");
}
public void wagTail() {
System.out.println("Tail wagging...");
}
public void begFood() {
System.out.println("Begging for food...");
}
}
```
其中,猫和狗类实现了Pet接口的所有方法,并且有各自独特的行为;猪和马类没有实现Pet接口,但仍然有各自独特的行为。
阅读全文