以下是OODesign的内容

时间:2011-06-06 16:26:16

标签: java oop

我被要求在我的一次采访中完成这个测试,他们希望我按照所有正确的做法创建课程。我无法理清这次采访,但我确定感兴趣的是你们所有的专家会如何解决这个问题。

为以下方案创建OODesign

有一个动物园,它有一些动物。 以下是生活在动物园里的动物 狮子 虎 熊

每只动物都有能量水平

每个动物说话,吃饭,玩耍 通过defalut动物吃,他们获得+8能量水平 默认情况下,当动物玩耍时它们会松散-5级能量 通过defalut当动物说话时他们松散-1

默认情况下,每只动物都说“Grrr ......” 默认情况下,每个动物玩耍时都会说“我喜欢它”

只有熊可以跳舞,但是当熊跳舞时,他说“看MA'我在跳舞”

以下是动物可用的食物 谷物,牛排和鲑鱼

在Zoo上调用soundOff()方法时 每个动物都说话

Tiger说话的时候说“ROARR ......” 当Lion说话时他说:“你不敢问我”

4 个答案:

答案 0 :(得分:2)

我会将Zoo结构化为包含一个Animal基类的集合:

class Zoo {
 private Collection<Animal> animals;

 ...
 public void soundOff() {
  Iterator<Animal> i = animals.iterator();
  while( i.hasNext() )
   i.next().speak();
 }

然后动物基类具有共同的方法/属性:说话,吃饭,玩耍,能量水平;也允许动物在eat()方法中拒绝他们不喜欢的食物:

abstract class Animal {
 private int energyLevel;
 private String playMessage;
 private String speakMessage;

 ...
 public Animal(String speakMessage, String playMessage) {
  this.speakMessage = speakMessage;
  this.playMessage = playMessage;
 }

 public Animal() {
  this( "Grrr...", "I am loving it" );
 }
 ...
 public abstract void eat( Food f ) throws IllegalArgumentException;

 public void play() {
  System.out.println( playMessage );
  energyLevel += 8;
 }

 public void speak() {
  System.out.println( speakMessage );
  energyLevel -= 1;
 }
..
}

那么你可以拥有专门的课程 - 比如DancerAnimal:

abstract class DancerAnimal extends Animal {
 private String danceMessage;

 public DancerAnimal(String speakMessage, String playMessage, String danceMessage) {
  super( speakMessage, playMessage );
  this.danceMessage = danceMessage;
 }

 public DancerAnimal( String danceMessage ) {
   super();
   this.danceMessage = danceMessage;
 }

 public void dance() {
  System.out.println( danceMessage );
 }
...
}

基于此,你可以实现熊:

class Bear extends DanceAnimal {
 public Bear() {
  super( "Look MA' I am dancing" );
 }
 ...
}

狮子和老虎只会扩展动物基类,而食物可能会是一个枚举。

我的2美分。 请记住,上面没有处理并发性 - 所以一个线程调用dance而其他调用play会破坏energyLevel - 但是你可以简单地在它周围添加一个Lock。

答案 1 :(得分:0)

不是所有你想要的东西,但这样的东西可以帮助你搞清楚,d

使用所有属性和行为共享动物类,然后声明为扩展基类的每个动物定义属性和行为的所有类:

abstract class Animal
{
   private int energyLevel;

   public Animal(int eLevel)
   {
      setEnergyLevel(eLevel);
   }

   public void setEnergyLevel(int eLevel)
   {
       this.energyLevel = eLevel;
       if(energyLevel <= 0)
       {
          energyLevel = 0;
       }
   }

   public void speak()
   {
       System.out.println("grr...");
       setEnergyLevel(energyLevel - 1);
   }

   public void eat()
   {
       setEnergyLevel(energyLevel + 8);
   }

   public void play()
   {
       System.out.println("I'm loving it...");
       setEnergyLevel(energyLevel - 5);
   }
}


class Lion extends Animal
{
    public Lion(int energy)
    {
        super(energy);
    }
}

class Bear extends Animal
{
    public Bear(int energy)
    {
        super(energy);
    }

    public void dance()
    {
        System.out.println("I am dancing...");
    }
}

class Tiger extends Animal
{
    public Tiger(int energy)
    {
        super(energy);
    }
}

答案 2 :(得分:0)

class AnimalZoo {
       public int energyLevel=0;
       public String defaultVoice = "Grrr";
       public String sing = "I am lovinig t"; 

       public static void main(String [] args) {
        Tiger t = new Tiger();
        Lion l = new Lion();    
        Bear b = new Bear();

        AnimalZoo az = new AnimalZoo();
        az.soundOff();  
        }

       public void soundOff() {
        t.speak();
        l.speak();
        b.speak();
       }
       public void eat { 
          energyLevel+=8;
        }

       public void play{
          energyLevel-=5;
        } 

       public void speak { 
        energyLevel-=1;
        }
    }
    }

class Lion extends AnimalZoo {
    defaultVoice = "Don't you dare ask me";
    System.out.printnln(defaultVoice);
}

class Tiger extends AnimalZoo { 
    public void speak { 
        defaultVoice = "ROARR";
        System.out.println(defaultVoice);
    }
}

class Bear extends AnimalZoo {
         public void dance() {
    System.out.println("Look MA' I am dancing");

}

答案 3 :(得分:0)

请尝试以下代码:

public abstract class Animal {

    int energyLevel = ZooAnimalConstants.DEFAULT_ENERGY;
    String speakMessage;
    String playMessage;

    public Animal(int energy) {
        this.setEnergyLevel(energy);
        speakMessage="Grr...";
        playMessage="I am loving it";

    }

    public void setEnergyLevel(int energy) {
        if (energy < 0) {
            this.energyLevel = ZooAnimalConstants.DEFAULT_ENERGY;
        } else {
            this.energyLevel = energy;
        }
    }

    public void speak() {
        System.out.println("speak message : "+speakMessage);
        this.setEnergyLevel(this.energyLevel - ZooAnimalConstants.MINUS_SPEAK_ENERGY);
    }

    public void eat() {
        this.setEnergyLevel(this.energyLevel + ZooAnimalConstants.ADD_EAT_ENERGY);
    }

    public void play() {
        System.out.println("play message : "+playMessage);
        this.setEnergyLevel(this.energyLevel - ZooAnimalConstants.MINUS_PLAY_ENERGY);
    }

    public int getEnergyLevel()
    {
        return this.energyLevel;
    }

}

舞蹈行为:

public interface DanceBehaviour {

    public abstract void dance();

}

public class Bear extends Animal implements DanceBehaviour{

    public Bear() {
        super(ZooAnimalConstants.BEAR_DEFAULT_ENERGY);
    }

    public void dance()
    {
        System.out.println("Look MA' I am dancing");
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "Bear";
    }

}

public class Tiger extends Animal {

    public Tiger() {
        super(ZooAnimalConstants.TIGER_DEFAULT_ENERGY);
        super.speakMessage = "ROARR ..";
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "Tiger";
    }

}

public class Lion extends Animal {

    public Lion() {
        super(ZooAnimalConstants.LION_DEFAULT_ENERGY);
        this.speakMessage = "Don't you dare ask me";
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "Lion";
    }

}


import java.util.ArrayList;


public class Zoo {

    ArrayList<Animal> listAnimal; 

    public Zoo()
    {
        listAnimal= new ArrayList<Animal>();
    }

    public void addAnimal(Animal animal)
    {
        if(null != listAnimal && null != animal)
        {
            listAnimal.add(animal);
        }
    }

    public void soundOff()
    {
        for(Animal animal : listAnimal)
        {
            animal.speak();
        }
    }

    public void printAnimalEnergyReport()
    {
        for(Animal animal : listAnimal)
        {
            System.out.println("Energy Level for : "+animal.toString() + " : "+animal.energyLevel);
        }
    }

}

public interface ZooAnimalConstants {

        int DEFAULT_ENERGY=0;
        int ADD_EAT_ENERGY = 8;
        int MINUS_SPEAK_ENERGY = 1;
        int MINUS_PLAY_ENERGY = 5;

        int TIGER_DEFAULT_ENERGY=5;
        int LION_DEFAULT_ENERGY=6;
        int BEAR_DEFAULT_ENERGY=4;

}


public class ZooDemoMain {

    public static void main (String args[])
    {
        Zoo myZoo = new Zoo();

        Animal tiger = new Tiger();
        Animal bear = new Bear();
        Animal lion = new Lion();

        myZoo.addAnimal(tiger);
        myZoo.addAnimal(lion);
        myZoo.addAnimal(bear);

        myZoo.soundOff();
        myZoo.printAnimalEnergyReport();

        bear.setEnergyLevel(0);

        if(bear.getEnergyLevel() == 0)
        {
            bear.eat();
            myZoo.printAnimalEnergyReport();
        }



    }

}