实验内容及要求:实现一个仿真农场,农场包括猫、狗、猪、马等动物。下面给出一个哺乳动物的 类,要求编写包括猫、狗、猪、马等动物的的类,并实现以下要求: 其中狗属哺乳动物,且它的属性有品种之分(在哺乳类基础上增加品种数属性),叫声区别于其他动 物(输出“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-04-27 19:19:44 浏览: 154
以下是实现猫、狗、猪、马等动物的类的代码:
public class Cat extends Mammal implements Pet{
public Cat(){
System.out.println("Cat constructor");
}
void Acts(){
System.out.println("Cat is scratching...");
}
void speak(){
System.out.println("Meow!");
}
public void TailWagging(){
System.out.println("Tail wagging...");
}
public void BeggingForFood(){
System.out.println("Begging for food...");
}
}
public class Dog extends Mammal implements Pet{
private String breed;
public Dog(String b){
breed = b;
System.out.println("Dog constructor");
}
void Acts(){
System.out.println("Dog is fetching...");
}
void speak(){
System.out.println("Woof!");
}
public void TailWagging(){
System.out.println("Tail wagging...");
}
public void BeggingForFood(){
System.out.println("Begging for food...");
}
}
public class Pig extends Mammal{
public Pig(){
System.out.println("Pig constructor");
}
void Acts(){
System.out.println("Pig is rolling in mud...");
}
void speak(){
System.out.println("Oink!");
}
}
public class Horse extends Mammal{
public Horse(){
System.out.println("Horse constructor");
}
void Acts(){
System.out.println("Horse is galloping...");
}
void speak(){
System.out.println("Winnie!");
}
}
接下来是Pet接口的代码:
public interface Pet{
void TailWagging();
void BeggingForFood();
}
注意,猫和狗类需要实现Pet接口的所有方法。
最后,我们可以通过以下代码来测试:
public class Farm{
public static void main(String[] args){
Mammal[] animals = new Mammal[4];
animals[0] = new Cat();
animals[1] = new Dog("Golden Retriever");
animals[2] = new Pig();
animals[3] = new Horse();
for(Mammal animal : animals){
animal.move();
animal.speak();
animal.Acts();
animal.sleep();
if(animal instanceof Pet){
Pet pet = (Pet)animal;
pet.TailWagging();
pet.BeggingForFood();
}
System.out.println();
}
}
}
输出结果应该如下:
Mammal constructor
Cat constructor
Mammal constructor
Dog constructor
Mammal constructor
Pig constructor
Mammal constructor
Horse constructor
Mammal move one step
Meow!
Cat is scratching...
shhh, I’m sleeping.
Tail wagging...
Begging for food...
Mammal move one step
Woof!
Dog is fetching...
shhh, I’m sleeping.
Tail wagging...
Begging for food...
Mammal move one step
Oink!
Pig is rolling in mud...
shhh, I’m sleeping.
Mammal move one step
Winnie!
Horse is galloping...
shhh, I’m sleeping.
阅读全文