使用Java补充程序 实验内容及要求:实现一个仿真农场,农场包括猫、狗、猪、马等动物。下面给出一个哺乳动物的 类,要求编写包括猫、狗、猪、马等动物的的类,并实现以下要求: 其中狗属哺乳动物,且它的属性有品种之分(在哺乳类基础上增加品种数属性),叫声区别于其他动 物(输出“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-02-10 17:09:41 浏览: 28
好的,以下是我写的猫、狗、猪、马等动物的类的代码,以及实现了Pet接口的猫和狗类的代码:
```
// Mammal类
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();
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.");
}
void Acts() {
System.out.println("Mammal acts!");
}
}
// 猫类
public class Cat extends Mammal implements Pet {
public Cat() {
System.out.println("Cat constructor");
}
@Override
public void wagTail() {
System.out.println("Tail wagging...");
}
@Override
public void begForFood() {
System.out.println("Begging for food...");
}
@Override
void speak() {
System.out.println("Meow!");
}
@Override
void Acts() {
System.out.println("Cat acts!");
}
}
// 狗类
public class Dog extends Mammal implements Pet {
private String breed;
public Dog(String breed) {
this.breed = breed;
System.out.println("Dog constructor");
}
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}
@Override
public void wagTail() {
System.out.println("Tail wagging...");
}
@Override
public void begForFood() {
System.out.println("Begging for food...");
}
@Override
void speak() {
System.out.println("Woof!");
}
@Override
void Acts() {
System.out.println("Dog acts!");
}
}
// 猪类
public class Pig extends Mammal {
public Pig() {
System.out.println("Pig constructor");
}
@Override
void speak() {
System.out.println("Oink!");
}
@Override
void Acts() {
System.out.println("Pig acts!");
}
}
// 马类
public class Horse extends Mammal {
public Horse() {
System.out.println("Horse constructor");
}
@Override
void speak() {
System.out.println("Winnie!");
}
@Override
void Acts() {
System.out.println("Horse acts!");
}
}
// Pet接口
public interface Pet {
void wagTail();
void begForFood();
}
```
以上是我根据实验要求写的代码,实现了一个仿真农场,包括猫、狗、猪、马等动物。其中,狗属于哺乳动物,具有品种属性,叫声为“Woof!”;猫、马、猪也属于哺乳动物,分别叫声为“Meow!”、“Winnie!”、“Oink!”。猫和狗类实现了Pet接口,具有摇尾巴和乞讨食物等宠物行为。每个动物类都表现出不同的行为,例如猫会“Meow!”、狗会“Woof!”、猪会“Oink!”、马会“Winnie!”,并且每个动物类都实现了父类Mammal的一些方法,如move()、speak()、sleep()等。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)