如何比较子类之间的变量来创建它。 Java的

时间:2017-06-29 19:10:13

标签: java inheritance constructor instance instance-variables

我们假设我得到一个字符串输入,可以是" 0"," brown"," .5m"或者" 50cm"。然后我创建一个dog类的狗实例,这是pet类的孩子。但pet类是horsepigbird的超类。

public class Pet {
    public final String[] attribs;
    public void sound() {
        System.out.print(attribs[4]);
    }
}

public class Dog extends pet {
    public final String[] attribs = {"0","brown",".5m","50cm","wow"};
}

public class Bird extends pet {
    public final String[] attribs = {"1","green",".3m","30cm","hola"};
}

public class Pig extends pet {
    public final String[] attribs = {"2","pink",".7m","75cm","oing"};
}

public class Horse extends pet {
    public final String[] attribs = {"3","black","1.5m","150cm","burf"};
}

public class app {
    public static void main(String[] arg) {
        Scanner inputs = new Scanner(System.in);
        String AttribInserted;
        Pet FindedPet;
        System.out.println("type an attribute of your pet")
        AttribInserted = inputs.nextLine();
        for(int i = 0; i < 4;i++)
            if (l.equals(dog.atbts[i])) FindedPet = new dog();
        for(int i = 0; i < 4;i++)
            if (l.equals(bird.atbts[i])) FindedPet = new bird();
        for(int i = 0; i < 4;i++)
            if (l.equals(horse.atbts[i])) FindedPet = new horse();
        for(int i = 0; i < 4;i++)
            if (l.equals(pig.atbts[i])) FindedPet = new pig();
        System.out.print("Your pet says:");
        FindedPet.sound();
    }
}

好吧,如果我添加更多的pet子类并为每个人添加属性,是否有办法比较所有子类的所有变量?例如: l.equals(subclass.atbts[i]) 大多数情况下,如果我想使用更复杂的项目。 subclass.atbts[i][j][k]

2 个答案:

答案 0 :(得分:0)

属性的Map<String, Object>怎么样?键将是属性的名称,值当然是属性的值。此Map可以是Pet中的一个字段,因此可以在所有子类中使用。

答案 1 :(得分:0)

首先,请以干净和可读的方式编写代码,并遵循正确的命名约定。类dog应为Dogatbts甚至不是任何单词的通用缩写。我猜你的意思是attributes

其次,设计简直是不合理的。

这些不是宠物的“属性”。您可能只是将其视为宠物类型的“别名”。但是我不知道50cm如何被视为狗的别名:狗可以有各种大小,还有其他可能有50厘米大小的宠物。如果没有这样的基本设计考虑,那么谈论“更复杂的项目”就毫无意义。我有点怀疑你误读了你的家庭作业。

但是,根据您的初衷,设计仍然可以稍微改进一下:

// pseudo-code

class Pet {
    protected String sound;   // these are what we called ATTRIBUTE!
    protected long size;
    // some other attributes too

    public Pet(long size, String sound /*other attributes too*/) {  
        this.size = size;
        ....
    }

    void makeSound() {
        print this.sound;
    }
}

class Dog {
    long DEFAULT_SIZE = 50;
    long DEFAULT_SOUND = "woof";
    public Dog() {
        super(DEFAULT_SIZE, DEFAULT_SOUND);
    }
    public static boolean hasAlias(String input) {
        // you can think of a way to check if input is an alias by,
        // for example, having a Set<String> which contains
        // DEFAULT_SIZE, DEFAULT_SOUND, pet name, sequence etc,
        // and simply do a aliases.contains(input);
}

// in main logic
String input = readInput();

// the following piece can be further refactored by having
// a factory, and each pet type register their alias etc...
if (Dog.hasAlias(input)) {
    pet = new Dog();
} else if Cat.hasAlias(input)) {
    pet = new Cat();
}

更新

在这个答案的评论中给出了额外的信息,这是一个更新的方法。

  

我的初衷并不是制作真正的动物类,我的   意图是我输入一些东西,应用程序知道我在说什么,如果   我输入“吃”我需要应用程序告诉我“Ate”已经过去了   不定形式是“吃”。如果我输入读取,那么应用程序告诉我   “阅读”是过去的,过去的分词和现在以及它的不定形式   是“阅读”

首先,您不应将每个“单词”表示为单独的类。在您的示例中,eatread应该是某个实例,而不是单独的类。

它应该是这样的:

class Word {
    Collection<String> getVariations();
}

class Verb extends Word {
    private String present;
    //....

    public Verb(String present, String past, String participle, String continuous) {
        this.present = present;
        //....
    }
}

在你的主逻辑中(虽然我会把它作为一个单独的类,如WordRepository):

class WordRepository {
    private Map<String, Word> wordVariationMap = new HashMap<>();

    void registerWord(Word word) {
        for (String variation: word.getVariations()) {
            wordVariationMap.put(variation, word);
        }
    }

    void mainLogic() {
        // create words and setup the lookup map
        registerWord(new Verb("eat", "ate", "eaten", "eating"));
        registerWord(new Verb("read", "read", "read", "reading"));

        String input = readInput();
    Word word = wordVariationMap.get(input);
    if (word) {  // if found
        word.doWhatever();
    }
}

}