简体   繁体   English

声明一个Java对象但调用子类的构造函数

[英]Declaring a Java object but calling the constructor of a child class

Imagine there's two Java classes, Dog & Poodle.假设有两个 Java 类,Dog 和 Poodle。 The Poodle class extends the Dog class. Poodle 类扩展了 Dog 类。

When creating an instance of these classes, this is the common syntax:创建这些类的实例时,这是常见的语法:

Dog myDog = new Dog();
Poodle myPoodle = new Poodle();

I don't understand what's happening though when you create an object of type parent class and then call the child class constructor, like so:我不明白当您创建父类类型的对象然后调用子类构造函数时发生了什么,如下所示:

Dog myDog = new Poodle();

If myDog is a Dog class, why would we call the constructor of one of its child classes, and what does that even mean?如果 myDog 是一个 Dog 类,我们为什么要调用它的一个子类的构造函数,这意味着什么? Why not just create an object of type child class (Poodle)?为什么不创建一个子类(贵宾犬)类型的对象?

To put another way, I don't know what this sentence means: "I'm going to create an object of type Dog but I'm going to call the constructor of one of its child classes, even though this object is not of the child class type".换句话说,我不知道这句话是什么意思:“我要创建一个 Dog 类型的对象,但我要调用它的一个子类的构造函数,即使这个对象不是子类类型”。

I can't make any logical sense of what this means.我无法理解这意味着什么。 Please help me understand what is going on here.请帮助我了解这里发生了什么。

EDIT - the linked duplicate question indeed appears to be asking the same question.编辑- 链接的重复问题确实似乎在问同样的问题。 Frustratingly, the two most upvoted answers on that page don't provide an explicit example of a meaningful use of the code segment that the questioner specifically asked about:令人沮丧的是,该页面上两个最受好评的答案并没有提供一个明确的例子,说明如何有意义地使用提问者特别询问的代码段:

Parent parent = new Child();

I understood and followed all code segments provided by the two most upvoted answers on the linked duplicate question.我理解并遵循了链接的重复问题上两个最受好评的答案提供的所有代码段。 I even understood where the polymorphism took place.我什至明白多态发生的地方。 But neither of the answers used the line of code specifically called out in the question.但是这两个答案都没有使用问题中特别提到的代码行。 I would really, really appreciate if someone could please show me an example of where如果有人能告诉我一个例子,我真的非常感激

Parent parent = new Child();

is used usefully and meaningfully in a broader segment of code.在更广泛的代码段中有用且有意义地使用。 Please.请。 I would really, really appreciate it.我真的,真的很感激。

The concept is called Dynamic method dispatch, by using this run time polymorphism(Overriding) is achieved in java.这个概念叫做动态方法分派,通过使用这个运行时多态性(覆盖)在java中实现。

Let's extend the example you wrote as below让我们扩展你写的例子如下

class Dog{

public void bark(){
// parent class implementation
System.out.println("I bark");
}

}

Here we have defined a Dog class which has a method called bark and provides it's implementation as well.这里我们定义了一个Dog类,它有一个叫做bark的方法并提供它的实现。

Let's say Dog is having two subclasses as Poodle and Chihuahua as below.假设Dog有两个子类: PoodleChihuahua ,如下所示。

class Poodle extends Dog{

@Override
public void bark(){
//Poodle implementation
System.out.println("I bark like Poodle");
}
}

class Chihuahua extends Dog{

@Override
public void bark(){
//Chihuahua implementation
System.out.println("I bark like Chihuahua");
}
}

Poodle and Chihuahua classes have their implementation of the bark method. PoodleChihuahua类都有bark方法的实现。

And you created a child object and assigned a parent reference to it and call bark method as below.并且您创建了一个子对象并为其分配了一个父引用并调用 bark 方法,如下所示。

Dog dog1 = new Poodle();
dog1.bark(); //prints **I bark like Poodle**

At the run time JVM understands that you have a Poodle Object and checks if Poodle overrides the bark method and then calls it.在运行时 JVM 知道您有一个Poodle对象并检查Poodle覆盖了 bark 方法然后调用它。

This pattern can be observed in Collections when you do something like below.当您执行以下操作时,可以在 Collections 中观察到这种模式。

List<String> list = new ArrayList<>();
list.add("test string"); //this gets added according to ArrayList implementation.

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

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