Java 继承、instanceof、转换 - 如何通过 parent object 接受 children object?

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

** UPDATE: Solved! **

I created abstract parent class with 2 methods, getWeight() and getName()

In Bird class, getName() will return the bird's name.
In Monkey class, getName() will call it's pets and return the result from whichever pet returns the name.

Then I just call getName() in root monkey class and let it find the name for me.

我有三个类:(Parent)动物,(Child)鸟和猴子

猴子有体重和两只宠物动物,可以是另一只猴子,也可以是鸟

鸟有体重和名字(weight, name)

它们一起构成了一棵树,叶子节点是鸟,non-leaf节点是猴子 (见图像)

//                                          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;

我想在这个问题中使用 parent-child 继承,但它不允许我访问 child object 的属性,因为我使用 parent object 变量中的声明。

我该如何解决这个问题?我想使用继承,但我就是弄不明白。 请帮忙。 我在下面的代码中也有一些小问题。如果有人可以帮助澄清这些,那将非常有帮助。

// 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;
    }
}