JAVA的題目,有人可以幫解答一下嗎?

請定義Roar介面(叫的介面),該介面將定義roaring()方法,並由Dog、Cat及Monkey三個類別實作此介面,每個類別實作roaring()方法時,將輸出不同的叫聲,Dog類別將輸出『汪汪、汪汪』、Cat類別將輸出『喵喵、喵喵』、Monkey類別則輸出『吱吱、吱吱』的三種叫聲,在主程式中個別實作三種物件,並分別輸出三種叫聲。

2 Answers

Rating
  • Favorite Answer

    interface Roar

    {

    void roaring();

    }

    class Dog implements Roar

    {

    public void roaring()

    {

    System.out.println("汪汪、汪汪");

    }

    }

    class Cat implements Roar

    {

    public void roaring()

    {

    System.out.println("喵喵、喵喵");

    }

    }

    class Monkey implements Roar

    {

    public void roaring()

    {

    System.out.println("吱吱、吱吱");

    }

    }

    public class xxx

    {

    public static void main(String[] args)

    {

    Dog d=new Dog();

    Cat c=new Cat();

    Monkey m=new Monkey();

    d.roaring();

    c.roaring();

    m.roaring();

    }

    }

    Source(s): Myself
  • 10 years ago

    public class Animal implements Roar{

    private String myroar;

    public Animal(String newroar){

    myroar=newroar;

    }

    public void roaring(){

    System.out.println(myroar+" "+myroar);

    }

    public static void main(String[] args) {

    Animal dog=new Animal("汪汪、汪汪");

    System.out.print("我是一隻狗 : ");

    dog.roaring();

    System.out.print("我是一隻貓 : ");

    Animal cat=new Animal("喵喵、喵喵");

    cat.roaring();

    Animal monkey=new Animal("吱吱、吱吱");

    System.out.print("我是一隻猴子 : ");

    monkey.roaring();

    }

    }

    interface Roar{

    public void roaring();

    }

Still have questions? Get your answers by asking now.