PTA-6-41 Animal抽象类和IAbility接口

题目:

已知有如下Animal抽象类和IAbility接口,请编写Animal子类Dog类与Cat类,并分别实现IAbility接口,另外再编写一个模拟器类Simulator调用IAbility接口方法,具体要求如下。

需要你编写的Dog子类:

实现IAbility接口

showInfo方法输出Dog的name、age,输出格式样例为:我是一只狗,我的名字是Mike,今年2岁(注意:输出结果中没有空格,逗号为英文标点符号)

cry方法输出Dog 的叫声,输出格式样例为:旺旺

需要你编写的Cat子类:

实现IAbility接口

showInfo方法输出Cat的name、age,输出格式样例为:我是一只猫,我的名字是Anna,今年4岁(注意:输出结果中没有空格,逗号为英文标点符号)

cry方法输出Cat 的叫声,输出格式样例为:喵喵

需要你编写的模拟器类Simulator:

void playSound(IAbility animal):调用实现了IAbility接口类的showInfo和cry方法。

题目给出已有的抽象类Animal、IAbility接口、Main类定义:

import java.util.Scanner;

abstract class Animal{
    private String name;  //名字
    private int age;   //年龄
    
    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }    
}
interface IAbility{
    void showInfo();  //输出动物信息
    void cry();    //动物发出叫声
}
/* 请在这里填写答案 */


public class Main {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        IAbility animal=null;
        int type=input.nextInt();
        String name=input.next();
        int age=input.nextInt();
        if (type==1)
            animal=new Dog(name,age);
        else
            animal=new Cat(name,age);
        
        Simulator sim=new Simulator();
        sim.playSound(animal);
        input.close();
    }
}

        根据题目要求,我们需要创建两个 Animal 的子类(Dog、Cat)来实现接口 IAbility一个模拟器类(Simulator)。子类Dog、Cat要求使用showInfo方法来输出name与age,用Cry方法来输出叫声。模拟器类 Simulator 用于调用实现IAbility接口类的showInfo和cry方法。

代码实现如下:

//Dog 和 Cat 继承父类 Animal,实现接口 IAbility
class Dog extends Animal implements IAbility{

    public Dog(String name, int age) {
        super(name, age);
    }

    @Override
    public void showInfo() {
        System.out.println("我是一只狗,我的名字是"+super.getName()+",今年"+super.getAge()+"岁");
    }
    @Override
    public void cry() {
        System.out.println("旺旺");
    }
}

class Cat extends Animal implements IAbility{

    public Cat(String name, int age) {
        super(name, age);
    }

    @Override
    public void showInfo() {
        System.out.println("我是一只猫,我的名字是"+super.getName()+",今年"+super.getAge()+"岁");
    }

    @Override
    public void cry() {
        System.out.println("喵喵");
    }

}

class Simulator {
    void playSound(IAbility animal) {
        animal.showInfo();
        animal.cry();
    }
}

注:编写 showInfo方法时需要注意输出格式不要错误。


以上就是 PTA-6-41 Animal抽象类和IAbility接口 的全部内容了,希望能对你有所帮助!

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
### 回答1: 好的,以下是回复: 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(); } } ``` 以上就是根据要求编写的代码,希望能够帮到您。 ### 回答2: animal抽象类和iability接口主要提供了动物的基本属性和行为,我们需要编写dog类和cat类来具体实现这些属性和行为。同时,需要编写一个模拟器类simulator来调用iability接口方法来模拟动物的行为。 首先,我们来编写dog类来具体实现animal抽象类和iability接口。dog类可以继承animal抽象类,并实现iability接口的方法。dog类可以包含以下属性:name、age和color,并实现以下方法:eat、sleep、run和sound。其中,sound方法用于狗的叫声。 接着,我们编写cat类来具体实现animal抽象类和iability接口。cat类也可以继承animal抽象类,并实现iability接口的方法。cat类同样可以包含以下属性:name、age和color,并且实现以下方法:eat、sleep、run和sound。其中,sound方法用于猫的叫声。 接下来,我们编写模拟器类simulator。simulator类可以调用iability接口的方法来模拟动物的行为。模拟器类可以包含一个方法simulate,通过传入一个animal类型的参数来模拟动物的行为。在simulate方法中,我们可以调用传入动物对象的方法来实现具体的行为模拟。 总结一下,根据题目要求,我们需要编写dog类和cat类来具体实现animal抽象类和iability接口,并再编写一个模拟器类simulator来调用iability接口方法来模拟动物的行为。在实现dog类和cat类的过程中,可以根据实际需要添加新的属性和方法。通过以上的设计和编码,我们可以实现题目要求的功能。 ### 回答3: Animal抽象类是一个表示动物的基类,IAbility接口是用来表示动物的能力的接口。 首先,我们需要编写Animal的子类Dog类和Cat类,并分别实现IAbility接口。具体代码如下: ```java public abstract class Animal { String name; int age; String color; public Animal(String name, int age, String color) { this.name = name; this.age = age; this.color = color; } public abstract void eat(); public abstract void sleep(); } public interface IAbility { void run(); void jump(); } public class Dog extends Animal implements IAbility { public Dog(String name, int age, String color) { super(name, age, color); } @Override public void eat() { System.out.println(name + "正在吃骨头"); } @Override public void sleep() { System.out.println(name + "正在睡觉"); } @Override public void run() { System.out.println(name + "正在奔跑"); } @Override public void jump() { System.out.println(name + "正在跳跃"); } } public class Cat extends Animal implements IAbility { public Cat(String name, int age, String color) { super(name, age, color); } @Override public void eat() { System.out.println(name + "正在吃鱼"); } @Override public void sleep() { System.out.println(name + "正在睡觉"); } @Override public void run() { System.out.println(name + "正在奔跑"); } @Override public void jump() { System.out.println(name + "正在跳跃"); } } ``` 接下来,我们需要编写一个Simulator类来调用IAbility接口的方法,并模拟动物的行为。具体代码如下: ```java public class Simulator { public static void main(String[] args) { Dog dog = new Dog("旺财", 3, "黑色"); Cat cat = new Cat("咪咪", 2, "橘色"); dog.eat(); dog.sleep(); dog.run(); dog.jump(); cat.eat(); cat.sleep(); cat.run(); cat.jump(); } } ``` 以上代码中,我们首先创建了一个名为旺财的Dog对象和一个名为咪咪的Cat对象,然后依次调用它们的eat、sleep、run和jump方法来模拟动物的行为。 希望以上回答能满足您的需求,如有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值