问题描述
请考虑以下内容:
public class parent {
int x;
parent() {
x = 5;
}
}
public class child extends parent {
int y;
child() {
super();
y = 10;
}
public static void main(String[] args) {
parent p = new child();
System.out.println(p.y);//can't access variable y
}
}
此处,在父类的对象上调用子类的构造函数.在进行Android编程时,我已经多次遇到这种情况. 我的问题是为什么允许这样做?子类的构造函数可能会初始化其他成员,但是它们的蓝图可能不会像上述情况那样在父类中定义.
Here a child class's constructor is being called on a parent class's object. I've come across this situation many times when doing Android programming. My question is why is this allowed? The child class's constructor might initialize additional members, but their blueprint might not be defined in the parent class like in the above case.
推荐答案
那不是那里发生的事情.在这一行:
That's not what's happening there. In this line:
parent p = new child();
...您正在创建child
对象,而不是parent
对象.用于引用它的变量p
具有类型parent
,这意味着您只能通过该引用使用parent
内容,但是该对象是child
对象.对象确实具有y
,您只是无法通过p
访问它.您可以这样做:
...you're creating a child
object, not a parent
object. The variable you're using to refer to it, p
, has the type parent
, which means that you can only use parent
stuff via that reference, but the object is a child
object. The object does have a y
, you just can't access it via p
. You can see that by doing this:
parent p = new child();
System.out.println( ((child)p).y );
通过告诉编译器,我们知道p
引用了child
,即使它的类型是parent
(通过"cast"),我们也可以访问它的child
特定部分. (当您有其他选择时,这不是一个好习惯.)
By telling the compiler we know that p
refers to child
even though it's of type parent
(via a "cast"), we can access the child
-specific parts of it. (This is not good practice when you have an alternative.)
这种对您所引用的内容与实际引用之间存在差异(在约束内)的能力称为多态性"(采用多种形式),并且是面向对象编程的核心.
This ability for there to be a difference (within constraints) between the reference you have to something and what it actually is is called "polymorphism" (taking multiple forms) and is central to object-oriented programming.
考虑:
class Animal {
}
class Dog extends Animal {
}
class Cat extends Animal {
}
所有Dog
和所有Cat
都是Animal
,但是Dog
不是Cat
.假设您经营一家宠物店,并希望存储手头动物的清单:
All Dog
s are Animal
s, as are all Cat
s, but Dog
s are not Cat
s. Suppose you run a pet shop and want to store a list of the animals you have on hand:
List<Animal> animals;
条目可以是Animal
类型或其任何子类型(例如Dog
和Cat
)的对象.这样,您就可以在Animal
上具有动物的常见方面,然后在这些类型上仅Dog
-或Cat
-特定方面(属性或行为).您可以查看Animal
个以动物性为依据的列表,而不必担心它们是Dog
还是Cat
s.
The entries can be objects of type Animal
or any of its subtypes, such as Dog
and Cat
. This lets you have common aspects of animals on Animal
, and then only Dog
- or Cat
- specific aspects (properties or behaviors) on those types. You can run through your list of Animal
s acting on their animal-ness without worrying about whether they're Dog
s or Cat
s.
这篇关于为什么允许使用子类的构造函数实例化父类对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!