簡體   English   中英

Java Inheritance, instanceof, cast - 如何接受孩子 object 通過父 ZA8CFDE6331149EB2ACZ66F96

[英]Java Inheritance, instanceof, casting - how to accept a children object throught parent object?

我有三個班級:(父母)動物,(孩子)鳥和猴子

猴子有體重和兩只寵物動物,可以是另一只猴子或鳥

鳥有重量和名字(重量,名字)

它們一起形成一棵樹,其中葉節點是鳥,非葉節點是猴子(見視覺)

//                                          Monkey, weight=40
//                                       /              \   
//                                      /                \                         
//                                     /                  \
//                                  Bird(5,"Big Bird")  Monkey,weight=25
//                                                        /           \
//                                                       /             \
//                                                      /               \
//                                                     /                 \
//                                       Bird(weight=7, name="BirdMan")  Bird(w=11, n="Stinky") 

在遞歸遍歷這棵樹以找到具有特定名稱的鳥時,我需要檢查當前節點是鳥還是猴子

// psuedo-is code
String recursive(Animal root, String target){

if (root instanceof Bird && root.name == target) return root.name;

// else, its not a Bird, its a Monkey
else 
    Animal left = root.left;
    Animal right = root.right;

    if (recursive(left) == target) return target;
    if (recursive(right) == target) return target;

    return "not found";

}

當我嘗試這樣做時,它說

error: cannot find symbol [in Main.java]
        Animal left = root.left;

我想在這個問題中使用父子 inheritance,但它不允許我訪問子 object 的屬性,因為我在變量中使用父 object 聲明。

我怎么解決這個問題? 我想使用 inheritance,但我就是想不通。 請幫忙。 我在下面的代碼中還有一些小問題。 如果有人可以幫助澄清這些,那將非常有幫助。

// animal parent class
class Animal {
   int weight;
   public Animal (int weight) { 
       this.weight = weight;
   }
}

// child class Bird, has weight & name
class Bird extends Animal{
   int name;
   public Bird (int weight, String name) {
       // * Question 1*
       // btw, is this line super(w) necessary? 
       //is it because the constructor of bird & animal have different args? 
       // do i have to say this.weight = weight;? or is that implied from super(w)? whats the most efficient way of declaring the inheritance i'm trying to establish?
       super(w);
       this.weight = weight;
       this.name = name;
   }
}

// child class Monkey, has weight & two pets (can be Monkey, or Bird)
class Monkey extends Animal{
   // *Question 2* Since animal can be both Monkey or Bird, I used parent class to do this.
   // is there a better way to do this?
   // I tried
   Animal left;
   Animal right;
   public Monnkey(int weight, Animal left, Animal right) {
       super(w);
       this.weight = weight;
       this.left = left;
       this.right = right;
   }
}

如果你想避免強制轉換,你可以在 Animal 類型上實現多態搜索:

class Animal {
    abstract Animal find(String name);
}

class Bird extends Animal {
    String name;

    @Override Animal find(String name) {
        if (this.name.equals(name)) return this;
        return null;
    }
}

class Monkey extends Animal {
    Animal left, right;

    @Override Animal find(String name) {
        Animal result = left.find(name);
        if (result == null) result = right.find(name);
        return result;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM