2017-06-29 6 views
-1

我們假設我得到一個字符串輸入,可以是「0」,「棕色」,「0.5米」或‘50釐米’。然後我創建dog類的狗實例,即pet類的孩子。但是,pet類是超類的horsepigbird如何比較子類之間的變量來創建它。 Java的

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(); 
    } 
} 

好吧,如果我增加寵物的個子類,我添加屬性給大家,是有辦法的所有子類的所有變量之間進行比較?例如: l.equals(subclass.atbts[i]) 而大多數,如果我想使用一個更復雜的項目。 subclass.atbts[i][j][k]

+1

請遵照Java命名約定。 'l'是文件特別警告不要使用的標識符。類型名稱應以大寫字母開頭。你有沒有注意到'dog.atbts'等等,不會編譯?另外,你完全忽視了在你的if和loop體上放置大括號('{','}')。 –

+2

@LewBloch *'l'是文檔專門警告不要使用的標識符。*您是否有源代碼? – shmosel

+0

我在梯子上,我從一種沒有約定的語言開始,我沒有上課,我從任何需要的地方閱讀,我不知道Java命名約定。此外,這個問題不是我的項目的引用,它只是一個簡單而通用的例子,易於理解和更有用。我從來沒有使用這個代碼,我沒有編譯它。 –

回答

0

怎麼樣Map<String, Object>的屬性。關鍵是屬性的名稱,值當然是屬性的值。該地圖可以是Pet內的一個字段,以便它可以在所有子類中使用。

+0

我不知道我是否不理解,但我需要什麼屬性名稱?我需要在屬性的基礎上創建一個實例,選擇一個類來創建它。那麼,我想你的意思是我可以將子類及其屬性映射到寵物類中的變量(我使用變量,因爲我不知道是否存在差異)。但是地圖是隻有兩個空格/列的表,還是可以擴展它? –

0

首先,請寫在乾淨和可讀的方式你的代碼,並遵循正確的命名約定。類dog應該是Dog,atbts甚至不是任何單詞的常用縮寫。我想你的意思是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(); 
} 

更新:

隨着這一評論給予額外的信息回答,這裏是一個更新的方法。

我的初衷並不是讓真的泛函動物類,我 意圖是我輸入的東西和應用程序知道我在說什麼,如果我 鍵入「吃」我需要的應用程序告訴我「吃」在過去和它的 不定式形式是「吃」。如果我型再現,然後應用程序告訴我 「讀」是在過去,過去分詞和現在和不定式形式 是「讀」

首先,你應該不能代表每一個「字」爲一個單獨的課程。在你的例子中,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(); 
    } 
} 

}

+0

我的初衷並不是製作真正的動物類,我的意圖是輸入一些東西,應用程序知道我在說什麼,如果我鍵入「吃」,我需要應用程序告訴我「吃」在過去和它的不定式形式是「吃」。如果我輸入閱讀,那麼應用程序告訴我''閱讀「已在過去,過去分詞和現在,其不定式形式是」閱讀「。 –

+0

而且'String input = readInput();'後面的代碼恰恰是我想要避免的,如果我有100個類,我需要100個if語句。 –

+0

明確你想要做什麼。確保你給出有意義的例子,這樣不會分散你真正的問題。順便說一句,即使是你在評論中提到的內容,爲'eat'和'read'創建單獨的類仍然沒有意義。無論如何,我會稍微更新一下我的答案,以滿足您在評論 –

相關問題