2012-03-11 59 views
3

它可能看起来像你的伪问题,但我有困难解决这个:简单的面向对象的编程概念

我们有一个抽象class AnimalCat和延伸它Dog。在Animal我们有一个方法produceSound();是抽象的。正如你可能已经猜到了Cat它应该返回“毛”和Dog - “宝”或类似的东西。这是确定的,但现在我们必须写一个返回取决于他们的声音CatDog对象Animal class一个static方法。例如:identifyAnimal("Mao")应该返回Cat

问题:如何实施identifyAnimal(String sound)方法?

下面是层次结构的一些简单的例子:

动物类

public abstract class Animal { 

    protected abstract String produceSound(); 

    protected static void identifyAnimal(String animalSound) { 
     // TODO 
    } 
} 

Cat类

public class Cat extends Animal{ 

    @Override 
    protected String produceSound() { 
     return "Mao"; 
    } 
} 

狗类

public class Dog extends Animal{ 

    @Override 
    protected String produceSound() { 
     return "Bao"; 
    } 
} 

测试类

public class AnimalTest { 

    public static void main(String[] args) { 
     Animal.identifyAnimal("Bao"); 
    } 
} 

AnimalTest类调​​用Animal.identifyAnimal("Bao");我们应该得到一个Dog时。

+0

你想dog.identifyAnimal( “毛”)返回...猫的一些实例或字猫?也许狗不应该知道关于猫的任何事情。但是如果它只知道“宝”,那么你真的在问,布尔dog.isYourSound(“宝”); 静态方法是没有任何实例。 – 2012-03-11 02:04:11

+0

想知道,猫在哪个国家说毛和狗宝? – rid 2012-03-11 02:04:54

+0

我刚刚编辑过这个问题。 – nyxz 2012-03-11 02:04:54

回答

2
private static Class[] animalTypes = [Dog.class, Cat.class]; 

public static String identifyAnimal(String animalSound) 
{ 
    for (int i = 0; i < animalTypes.length; i++) { 
     Animal a = animalTypes[i].newInstance(); 
     String s = a.produceSound(); 

     if (animalSound.equals(s)) 
      return animalTypes[i].getName(); 
    } 

    return null; 
} 
+0

愿你与@ AdamReed的解决方案来合并是金色的中间 – nyxz 2012-03-11 02:31:40

0

做与你,让每个声音返回一个字符串,用动物的名字预定义的声音的开关。例如,“毛”返回一个字符串“猫”等。 还有一两件事让乌尔identifyAnimal方法returna字符串,而不是无效的。

+0

你的方法应该有一个字符串参数偏离航向,不应该是静态的我猜 – 2012-03-11 02:09:53

0

您可以使用反射来获取扩展Animal的所有类型的列表,使用Activator循环它们以创建每个运行produceSound的每个类型的实例,直到找到与animalSound匹配的返回值,并返回该实例。如果你想避免你的动物类意识到扩展它的内容,那么速度缓慢但很有效。

+0

哎呦没有看到Java标记,我是假设C# – 2012-03-11 02:57:41

0

您试图解决的问题的性质是什么?没有与问题无关的“正确”方式。

什么是消费类应用需要Animal?你的应用程序需要使用它所使用的类是什么?除非这些推定是明确的,否则不能假定。

2

所以这里有一个(可怕的)方法来做到这一点。我实际上抽动了一下。 我不知道你在用什么语言,所以我会用C++(抱歉,当前模式),尽管你可以用Dictionarys替换地图,如果我们在C#中,无论如何。这是一个不好的方法去做事,但应该工作(在概念上,无论如何)

再次...可怕......

公共抽象类动物{

protected abstract String produceSound(); 

    protected static map<string, string> SoundList; 
    protected static bool registerSound(string sound, string type) 
    { 
      return (SoundList.insert(pair<string, string>(sound, type)))->second;//true if worked false if already there 

    } 

    protected static string identifyAnimal(string animalSound) 
    { 
      map<string,string>::iterator result = SoundList.find(sound); 
      if(result != SoundList.end()) 
       return result->second; 
      else 
       return "What The Hell Is This!?"; 
    } 
} 
Cat class 

public class Cat extends Animal 
    { 
     Cat() 
     { 
       Animal::registerSound("Mao","Cat"); 
     } 

    @Override 
    protected String produceSound() { 
     return "Mao"; 
    } 
} 
+0

刚读Java标签...可能会与Manishs解决方案然后 – 2012-03-11 02:27:18

+0

或组合,注册Classes而不是字符串。虽然这仍然存在无用的开销问题(在第一次注册表/课程之后) – 2012-03-11 02:30:48

1
abstract class Animal { 
    static Map<String,String> map = new HashMap<String,String>(); 
    public Animal(String value) { map.put(produceSound(), value); } 
    protected abstract String produceSound(); 
    protected static void identifyAnimal(String animalSound) { 
     System.out.println(map.get(animalSound)); 
    } 
} 

class Cat extends Animal { 
    @Override 
    protected String produceSound() { return "Mao"; } 
    Cat(){ super("CAT"); } 
} 

class Dog extends Animal { 
    @Override 
    protected String produceSound() { return "Bao"; } 
    Dog(){ super("DOG"); } 
} 

class Test { 
    public static void main(String[] args) { 
     new Dog();   
     new Cat(); 
     Animal.identifyAnimal("Bao"); 
    }  
}