-2

假设我们有这个类

动物.java

public class Animal {

    private String name;

    public Animal(String name) {
        this.name = name;
    }

    public void shout() {
        /* must be override */
    }

    public String getName() {
        return name;
    }
}

驱动程序.java

import java.util.ArrayList;
import java.util.List;

public class Driver {
    public static void main(String[] args) {

    List<Animal> animals = new ArrayList<Animal>();
    animals.add(new Animal("Dog") {

        @Override
        public void shout() {
            System.out.println(getName() + " sounds like: woof woof woof");
        }
    });

    animals.add(new Animal("Cat") {

        @Override
        public void shout() {
            System.out.println(getName() + " sounds like: meow meow meow-ow");
        }
    });

    for(Animal a : animals)
        a.shout();
    }
}

我怎样才能让我实现方法喊()或任何其他程序员?假设没有理由将 Animal 类抽象化,并为 Dog、Cat 等创建一个扩展 Animal 的类

有设计模式吗?

4

4 回答 4

0

您可以shout()在基类中实现并添加voice()返回动物声音的方法。

像这样:

public class Animal {

private String voice;

private String name;

public Animal(String name, String voide) {
    this.name = name;
    this.voice = voice;
}

public void shout() {
    System.out.println(getName() + " sounds like: "+ getVoice());
}

public String getName() {
    return name;
}

public String getVoice() { return voice }
}

在创建动物的代码中:

animals.add(new Animal("Dog", "woof"));

animals.add(new Animal("Cat", "meow");
于 2013-07-09T19:39:36.627 回答
0

如果您希望 Cat 和 Dog 做 Animal 所做的事情,而无需抽象动物,您可以。

public class Cat extends Animal

如果 Animal 是 Abstract 或一个正常的、完全实现的类,这将起作用。您将使用实现而不是扩展接口。

于 2013-07-09T19:44:09.333 回答
0

嗯,策略模式可以帮助你。

public class Animal {

   private Shoutable shoutable;  
   private String name;

    public Animal(String name) {
        this.name = name;
    }

    public void shout() {
        shoutable.shout();
    }

    public String getName() {
        return name;
    }

    public void setShout(Shoutable shoutable){
       this.shoutable=shoutable;
    }
}


public interface Shoutable{
  void shout();
}

public class DogShout implements Shoutable{
  @Override
  public void shout{
   System.out.println("woff gua guaa gua woff");
  }
}
于 2013-07-09T19:44:13.693 回答
0

您可以执行以下操作:

public class Animal {

    private String name;
    private Shoutable shoutable;

    public Animal(String name, Shoutable shoutable) {
        this.name = name;
        this.shoutable = shoutable;
    }

    public void shout() {
        shoutable.shout();
    }

    public String getName() {
        return name;
    }
}

Shoutable 接口:

public interface Shoutable {
    void shout();
}

并有一个可喊的实现作为匿名内部类:

import java.util.ArrayList;
import java.util.List;

public class Driver {
    public static void main(String[] args) {

    List<Animal> animals = new ArrayList<Animal>();
    animals.add(new Animal("Dog", new Shoutable() {
              @Override
              public void shout() {
                   System.out.println(getName() + " sounds like: woof woof woof");
              }
         });

    animals.add(new Animal("Cat", new Shoutable() {

        @Override
        public void shout() {
            System.out.println(getName() + " sounds like: meow meow meow-ow");
        }
    });

    for(Animal a : animals)
        a.shout();
    }
}
于 2013-07-09T19:52:48.683 回答