已知有如下Animal抽象类和IAbility接口,请编写Animal子类Dog类与Cat类,并分别实现IAbility接口,另外再编写一个模拟器类Simulator调用IAbility接口方法,具体要求如下。
时间: 2024-04-08 12:35:20 浏览: 93
抽象类和接口
好的,以下是回复:
animal抽象类:
```
public abstract class animal {
protected String name;
protected int age;
protected String gender;
public animal(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public abstract void eat();
public abstract void sleep();
public abstract void move();
}
```
iability接口:
```
public interface iability {
public void play();
public void bark();
}
```
dog类:
```
public class dog extends animal implements iability {
public dog(String name, int age, String gender) {
super(name, age, gender);
}
@Override
public void eat() {
System.out.println(name + " is eating bones.");
}
@Override
public void sleep() {
System.out.println(name + " is sleeping in the dog house.");
}
@Override
public void move() {
System.out.println(name + " is running in the park.");
}
@Override
public void play() {
System.out.println(name + " is playing fetch.");
}
@Override
public void bark() {
System.out.println(name + " is barking loudly.");
}
}
```
cat类:
```
public class cat extends animal implements iability {
public cat(String name, int age, String gender) {
super(name, age, gender);
}
@Override
public void eat() {
System.out.println(name + " is eating fish.");
}
@Override
public void sleep() {
System.out.println(name + " is sleeping on the sofa.");
}
@Override
public void move() {
System.out.println(name + " is jumping on the table.");
}
@Override
public void play() {
System.out.println(name + " is playing with a ball of yarn.");
}
@Override
public void bark() {
System.out.println(name + " is meowing softly.");
}
}
```
simulator类:
```
public class simulator {
public static void main(String[] args) {
dog myDog = new dog("Buddy", 3, "male");
cat myCat = new cat("Fluffy", 2, "female");
myDog.eat();
myDog.sleep();
myDog.move();
myDog.play();
myDog.bark();
myCat.eat();
myCat.sleep();
myCat.move();
myCat.play();
myCat.bark();
}
}
```
以上就是根据要求编写的代码,希望能够帮到您。
阅读全文