Sure, here’s the translation: 如何获取子类对象的引用类别

huangapple go评论61阅读模式
英文:

How to get the reference class of an subclass object

问题

以下是翻译好的内容:

我正在学习多态性,但似乎无法解决这个问题。我找不到正确的方法来检查我的对象是作为超类还是子类引用。基本上,我想利用一个方法来执行某些操作,取决于引用类,而不是对象类。在这个示例中,我在if条件中使用了 "instanceof",但这当然是错误的,因为两者都将列为Animal...

主类:

public class Main {
    public static void main(String[] args) throws FileNotFoundException {
        Animal dog1 = new Dog();
        Dog dog2 = new Dog();

        List<Animal> animalList = new ArrayList<>();
        animalList.add(dog1);
        animalList.add(dog2);

        for (Animal animal : animalList) {
            if (animal instanceof Animal) {
                animal.talk();
            } else if (animal instanceof Dog) {
                animal.talk();
                ((Dog) animal).walk();
            }
        }
    }
}

Animal类:

public class Animal {
    public void talk() {
        System.out.println("你好,我是一只动物");
    }
}

Dog类:

public class Dog extends Animal {
    public void walk() {
        System.out.println("狗可以走路");
    }
}
英文:

I am getting into polymorphism but I can't seem to solve this issue. I can't seem to find the correct way of checking whether my object is referenced as the superclass or a subclass. Basically I want to utilize a method to do certain things, depending on the reference class, and NOT the object class. In this example I used "instanceof" in the if condition, but that is wrong of course since both will be listed as Animal...

Main class:

    public class Main {
    public static void main(String[] args) throws FileNotFoundException {
        Animal dog1 = new Dog();
        Dog dog2=new Dog();

        List&lt;Animal&gt; animalList = new ArrayList&lt;&gt;();
        animalList.add(dog1);
        animalList.add(dog2);

        for (Animal animal:animalList) {
            if (animal instanceof Animal){
                animal.talk();
            }
            else if(animal instanceof Dog){
                animal.talk();
                ((Dog)animal).walk();
            }
        }
    }
}

Animal class:

    public class Animal {
    public void talk(){
    System.out.println(&quot;Hello I&#39;m an animal&quot;);
    }
  }

Dog class:

public class Dog extends Animal{
public void walk(){
    System.out.println(&quot;Dogs can walk&quot;);
  }
}

答案1

得分: 0

我会尝试类似这样的代码:

Animal animal1 = new Animal();
Animal animal2 = new Dog();

List<Animal> animalList = new ArrayList<>();
animalList.add(animal1);
animalList.add(animal2);

for (Animal animal : animalList) {
    animal.talk();
    if (animal instanceof Dog) {
        ((Dog)animal).walk();
    }
}
英文:

I would try something like this:

Animal animal1 = new Animal();
Animal animal2 = new Dog();

List&lt;Animal&gt; animalList = new ArrayList&lt;&gt;();
animalList.add(animal1);
animalList.add(animal2);

for (Animal animal : animalList) {
	animal.talk();
	if(animal instanceof Dog){
		((Dog)animal).walk();
	}
}

答案2

得分: 0

不确定它是否有帮助。
在Java中,您可以使用不同的参数定义相同的方法。每个对狗对象的引用将被传递给 do(Dog dog) 方法。对其他动物使用 do(Animal animal)
但这仍然是基于对象类型,而不是引用。在您的逻辑中,所有引用都应该是 Animal,因为您正在遍历一个 Animal 列表。
在 for 循环中,不再与 dog1 变量有关。

public class Main {
    public static void main(String[] args) throws FileNotFoundException {
        Animal dog1 = new Dog();
        Dog dog2 = new Dog();

        List<Animal> animalList = new ArrayList<>();
        animalList.add(dog1);
        animalList.add(dog2);

        for (Animal animal : animalList) {
            do(animal);
        }
    }

    public static void do(Animal animal) {
        animal.talk();
    }

    public static void do(Dog dog) {
        dog.talk();
        dog.walk();
    }
}
英文:

Not sure it can help.
In java you can define the same method with different parameters. Every reference to a dog object will be passed to the do(Dog dog) method. Every other animal to do(Animal animal)
But this is still based on the object type, not the reference. In your logic all references should be Animal as you are walking through a list of Animal.
Within the for loop there is no link to the dog1 variable anymore.

public class Main {
    public static void main(String[] args) throws FileNotFoundException {
        Animal dog1 = new Dog();
        Dog dog2=new Dog();

        List&lt;Animal&gt; animalList = new ArrayList&lt;&gt;();
        animalList.add(dog1);
        animalList.add(dog2);

        for (Animal animal:animalList) {
            do(animal);
        }
    }
    public static void do(Animal animal) {
        animal.talk();
    }
    public static void do(Dog dog) {
       dog.talk();
       dog.walk();
    }
}

答案3

得分: 0

Here's the translated code portion:

如果每只 `Dog` 每次说话都会走路那么 `Dog` 应该变成

public class Dog extends Animal {
    @Override
    public void talk(){
        super.talk();
        walk();
    }

    public void walk() {
        System.out.println("狗可以走路");
    }
}
然后你的 `main` 应该变成

public class Main {
    public static void main(String[] args) {
        Animal dog1 = new Dog();
        Dog dog2 = new Dog();
        Animal plain = new Animal(); // 添加一个新的普通动物用于测试。

        List<Animal> animalList = new ArrayList<>();
        animalList.add(dog1);
        animalList.add(dog2);
        animalList.add(plain);

        for (final Animal animal: animalList)
            animal.talk();
    }
}

至于你的代码问题是每个 Animal 引用都指向一个 Animal 实例(或任何继承它的类)或 null。你可以通过改变 if 条件的顺序来修复它,如下所示:

for (Animal animal: animalList) {
    if (animal instanceof Dog) {
        animal.talk();
        ((Dog)animal).walk();
    }
    else
        animal.talk();
}

对应于:

for (Animal animal: animalList) {
    animal.talk();
    if (animal instanceof Dog)
        ((Dog)animal).walk();
}
英文:

If every Dog walks every time it talks, then Dog should become:

public class Dog extends Animal {
    @Override
    public void talk(){
        super.talk();
        walk();
    }

    public void walk() {
        System.out.println(&quot;Dogs can walk&quot;);
    }
}

and your main should then become like:

public class Main {
    public static void main(String[] args) {
        Animal dog1 = new Dog();
        Dog dog2 = new Dog();
        Animal plain = new Animal(); //Added a new plain Animal for testing.

        List&lt;Animal&gt; animalList = new ArrayList&lt;&gt;();
        animalList.add(dog1);
        animalList.add(dog2);
        animalList.add(plain);

        for (final Animal animal: animalList)
            animal.talk();
    }
}

As for your code, the problem is than every Animal reference points to either an Animal instance (or any class which extends it), or null. You could fix it by changing the order of the if conditions like so:

for (Animal animal: animalList) {
    if (animal instanceof Dog) {
        animal.talk();
        ((Dog)animal).walk();
    }
    else
        animal.talk();
}

which corresponds to:

for (Animal animal: animalList) {
    animal.talk();
    if (animal instanceof Dog)
        ((Dog)animal).walk();
}

huangapple
  • 本文由 发表于 2020年8月11日 16:32:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63354451.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定