为什么可以在Java中将超类初始化为子类? [英] Why can a super class be initialized as a child class in Java?
问题描述
简而言之,这是可能的以及为什么的:
In a nutshell, how and why is this possible:
Object obj=new MyClass();
Object是所有对象的超类,因此MyClass是Object的子类.通常,在Java中,为什么可以在父类中使用子类的构造函数?
Object is the superclass of all objects, therefore MyClass is a child class of Object. In general, in Java, Why is it possible to use the constructor of a child class in the parent class?
我理解这是怎么回事,因为孩子拥有父类的所有变量/方法,所以在初始化它们时,您只是在初始化父构造函数中指定的变量,这些变量在定义中存在这个孩子.问题是,当您沿相反方向走时,不一定是正确的. 孩子可以有父母没有的变量,那么当父母甚至根本没有变量时,怎么可能将孩子构造函数与父母一起使用?
I understand how it could go the other way around, since the child has all the variables/methods of the parent class, so when you initialize them you are just initializing the variables specified in the parent constructor, that exist by definition in the child. The problem is, when you go the other way around, it is not necessarily true. A child can have variables the parent doesn't, so how is it possible to use the child constructor with the parent, when the parent does not even have the variables in the first place?
此功能在开发中有什么用途?我认为,如果要使用类B的实例,可以将其声明为B something = new B(),而不是A something =新的B().这可能是我的经验不足,因此,我对启发为什么将父类初始化为子类之一以及将其初始化为子类之一很有帮助.
What uses does this feature have in development? I would think that if you want an instance of class B, you would declare it as B thing=new B(), and not A thing=new B(). This is probably my inexperience talking, so I would appreciate enlightenment on why and how a parent class can be initialized as one of its children.
推荐答案
为什么可以在 家长班?
Why is it possible to use the constructor of a child class in the parent class?
这是不正确的.当你做
Object obj = new MyClass();
Object obj;
声明类型为Object
的引用
并且new MyClass();
返回对其创建的对象的引用.
Object obj;
declares a reference of the type Object
and new MyClass();
returns a reference to the object it created.
因此,您要实例化MyClass
并将引用分配给创建的对象,并将其分配给类型为Object
的引用,这是可能的,因为MyClass
是Object
.
So, you are instantiating a MyClass
and assigning the reference to the object created to a reference of the type Object
, and this is possible because MyClass
is an Object
.
如您所说,
孩子可以有父母没有的变量
A child can have variables the parent doesn't
这称为扩展父级功能(继承).
That's called extending the parent functionality (inheritance).
对于第二个问题,请考虑经典的Animal
示例:假设您创建了Animal
类,并在其上创建了方法makeSound()
.
For your second question think about the classic Animal
example: Suppose you create a Animal
class and you create a method makeSound()
on it.
现在,您将创建两个Animal
子类,分别是Dog
和Cat
,它们覆盖了Animal
的makeSound()
方法(Dog
吠声和Cat
喵叫声).
Now you create two subclasses of Animal
, Dog
and Cat
, that overrides the makeSound()
method of Animal
(a Dog
barks and a Cat
meows).
想象一下,您使用List
代表一个充满Animal
s(Dog
s和Cat
s)的房间,而您想将它们全部制成makeSound()
.您的列表将被声明为List<Animal>
,因为您不知道要存储的Animal
类型.
Imagine that you represent a room full of Animal
s (Dog
s and Cat
s) using a List
, and you want to make all of them makeSound()
. Your list will be declared as List<Animal>
because you don't know the kind of Animal
s that you will store.
然后遍历List
以为每个Animal
调用makeSound()
. Animal
是Dog
还是Cat
都没有关系,它将发出声音.
And then you iterate over the List
to call makeSound()
for each Animal
. It doesn't matter if the Animal
is a Dog
or a Cat
, it will make it's sound.
,然后想象您想将Bird
添加到List
.很简单,不是吗?
And then imagine you want to add Bird
s to the List
. Easy, isn't it?
这篇关于为什么可以在Java中将超类初始化为子类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!