将子类对象分配为超类对象时会发生什么 [英] What happens when a subclass object is assigned as a superclass object
问题描述
所以我有一个关于超类A和子类B的问题,其中A有2个公共变量,而B还有1个.
So I have a question about a superclass A and subclass B, where A has 2 public variables and B has 1 more.
我看到了这段代码:
A a = new A();
B b = new B();
a = b;
最后一行是做什么的?我不太了解在继承关系的两个类之间使用"="时会发生什么.
What does that last line do? I don't really understand what actually happens when you use "=" between 2 classes in an inheritance relationship.
推荐答案
这是简单的任务. =
是赋值运算符.
It's simple assignment. =
is an assignment operator.
让我们清除以下几点.
- 在Java中,创建对象并可以通过其访问时.参考.引用是指对象.
- 一次引用只能引用一个对象
- 类型X的引用可以引用类型X的对象或任何子类型(在X是类的情况下扩展,或者在X是接口的情况下实现).
现在假设有两个类Super
和Sub
,使得Sub extends Super
.
Now Suppose there are two classes Super
and Sub
such that Sub extends Super
.
SuperClass reference = new SubClass();
这是允许的,因为SubClass是从SuperClass继承的.
上面我们在堆中创建了一个SubClass类型的对象,可以通过访问该对象.引用为reference
This is allowed as SubClass inherits from SuperClass.
Above we have an object of type SubClass created in the heap and it is accessible via. reference named reference
请注意,类型为SubClass
的引用不能引用SuperClass
的对象.让我们简单地看看为什么会这样吗.如果允许类型SubClass
的引用引用类型为SuperClass
的对象,则将允许它调用SubClass
定义的其他方法(函数)(SubClass
将继承SuperClass
的所有方法并且还定义了一些其他方法).现在这将使应用程序崩溃,因为SuperClass
的对象仅具有SuperClass
中定义的方法,而没有SubClass
所定义的任何其他方法.因此,编译器会在编译时阻止它.引用类型为SuperClass
Note that a reference of type SubClass
can not refer to object of SuperClass
. Lets see in brief why so ?. If the reference of type SubClass
was allowed to refer an Object of type SuperClass
then it would have been allowed to invoke additional methods (functions) defined by SubClass
(SubClass
would have inherited all methods of SuperClass
and would also have defined few additional methods). Now this would have made the application to crash as the Object of SuperClass
only has methods defined in SuperClass
but does not have any additional methods defined by SubClass
. Hence compiler prevents it during compile time. Its a compile time error to have a reference of type SubClass
referring to an object of type SuperClass
现在让我们看问题中提到的代码
Now lets look at the code as mentioned in the question
SuperClass a = new SuperClass();
SubClass b = new SubClass();
a = b;
第1行:我们有一个SuperClass对象,该对象由名为a
Line 1 : We have an object of SuperClass being referred by a variable of type SuperClass named a
第2行:我们有一个SubClass对象,该对象由名为b
Line 2 : We have an object of SubClass being referred by a variable of type SubClass named b
第3行:我们有一个分配,其中分配了a
来引用与b
所引用的相同的对象.因此,现在我们两个引用都引用了在第2行创建的类型为SubClass
的对象.在第1行创建的类型为SuperClass
的对象(问题中提到了当前可用的代码)没有任何引用,因此它是合格的进行垃圾收集.
Line 3 : We have an assignment where a
is assigned to refer the same object as referred by b
. So now we have both references referring to the object of type SubClass
created at line 2. Object of typer SuperClass
created at line 1 (with the current available code mentioned in the question) is not having any references so it is eligible for garbage collection.
这篇关于将子类对象分配为超类对象时会发生什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!