简体   繁体   English

多态性 - object 和类型

[英]Polymorphism - object and type

I'm a bit confused on the concept of 'Subclass referenced as Superclass' within polymorphism.我对多态中的“子类引用为超类”的概念有点困惑。 (referencing here: https://stackify.com/oop-concept-polymorphism/ ) (此处参考: https://stackify.com/oop-concept-polymorphism/

Lets say we have the superclass animal and the subclass dog, where dog extends animal.假设我们有超类动物和子类狗,其中狗扩展了动物。 The following work:以下工作:

  1. animal testSuper = new animal();动物测试超级=新动物();
  2. dog testDog = new dog();狗 testDog = 新狗();
  3. animal testSuperDog = new dog();动物 testSuperDog = new dog();

Could anyone explain a bit further on whats happening behind the scenes for #3?任何人都可以进一步解释#3幕后发生的事情吗? When we do 'new dog()', are we creating an object of the dog class but when we do 'animal testSuperDog' we cast it to the superclass animal?当我们执行“new dog()”时,我们是否创建了狗 class 的 object,但是当我们执行“animal testSuperDog”时,我们将其转换为超类动物? Or is it the other way around - 'animal testSuperDog' creates an animal object but we're casting it down to the subclass dog when we do 'new dog()'?或者是相反的方式 - 'animal testSuperDog' 创建了一个动物 object 但我们在执行 'new dog()' 时将其转换为子类 dog?

I've tried the 4th permutation to explore and I get a type mismatch error saying that it can't convert from animal to dog.我尝试了第四个排列来探索,我得到一个类型不匹配的错误,说它不能从动物转换为狗。 So that's why I'm assuming there's some conversion going on.所以这就是为什么我假设正在进行一些转换。 4. dog testSubdog = new animal(); 4. 狗 testSubdog = new animal();

If we can dig a bit deeper, since we know #3 works, whats the benefit / use case of this?如果我们可以深入挖掘,既然我们知道 #3 有效,那么这样做的好处/用例是什么?

  1. testDog.noise(); testDog.noise();
  2. testSuperDog.noise(); testSuperDog.noise();

Both these would use the 'noise' method from the subclass dog.这两个都将使用子类 dog 的 'noise' 方法。

Polymorphism, the property of an object to take on many different forms.多态性,一个 object 的属性,具有许多不同的 forms。 To put this more precisely, a Java object may be accessed using a reference with the same type as the object, a reference that is a superclass of the object, or a reference that defines an interface the object implements, either directly or through a superclass. To put this more precisely, a Java object may be accessed using a reference with the same type as the object, a reference that is a superclass of the object, or a reference that defines an interface the object implements, either directly or through a superclass .

So for your first question animal testSuperDog = new dog();所以对于你的第一个问题animal testSuperDog = new dog(); java creates a dog object and the reference is an animal s you can only use methods in animal object. java 创建了一个狗 object 并且引用是一个动物你只能使用动物 object 中的方法。

side note: class name should start with a capital letter旁注: class 名称应以大写字母开头

For the second question you created a dog object and must behave as a dog so it use the overridden method and noway to use method in superclass对于第二个问题,您创建了一条狗 object 并且必须表现得像一条狗,因此它使用被覆盖的方法,而不能在超类中使用方法

An example to understand polymorphism:一个理解多态性的例子:

public class Reptile {
  public String getName() {
     return "Reptile";
  }
}
public class Alligator extends Reptile {
  public String getName() {
    return "Alligator";
  }
}
public class Crocodile extends Reptile {
  public String getName() {
    return "Crocodile";
  }
}
public class ZooWorker {
  public static void feed(Reptile reptile) {
    System.out.println("Feeding reptile "+reptile.getName());
  }

  public static void main(String[] args) {
    feed(new Alligator());
    feed(new Crocodile());
    feed(new Reptile());
  }
}

This code compiles and executes without issue, yielding the following output:此代码编译和执行没有问题,产生以下 output:

Feeding: Alligator
Feeding: Crocodile
Feeding: Reptile

If we can dig a bit deeper, since we know #3 works, whats the benefit / use case of this?如果我们可以深入挖掘,既然我们知道 #3 有效,那么这样做的好处/用例是什么?

Assume that you have a Person class:假设您有一个人 class:

public class Person {
    String name;
    Animal pet;

    public Person(String name, Animal pet) {
        this.name = name;
        this.pet = pet;
    }
}

Pet can be dog, cat or other宠物可以是狗、猫或其他

Animal dog = new Dog();
Animal cat = new Cat();
Persion john = new Persion("john", dog);
Persion lia = new Persion("lia", cat);

Class Cat and Dog must be extend Animal Class 猫和狗必须是扩展动物

If you want have an animal list and set it in the loop?如果您想要一个动物列表并将其设置在循环中?

Animal dog = new Dog();
Animal cat = new Cat();
List<Animal> list = new ArrayList<>();
list.add(cat);
list.add(dog);

for(Animal animal : list)
    animal.noise();

And there are many other cases.还有很多其他的情况。 You can reseach for:您可以研究:

  1. Solid design principles扎实的设计原则
  2. Dependency Injection of Spring framework Spring框架的依赖注入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM