为什么允许使用子类的构造函数实例化父类对象?

编程入门 行业动态 更新时间:2024-10-26 18:29:25
本文介绍了为什么允许使用子类的构造函数实例化父类对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

请考虑以下内容:

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.

推荐答案

为什么允许使用子类的构造函数实例化父类对象?

Why is it allowed for a parent class object to be instantiated using a child class's constructor?

那不是那里发生的事情.在这一行:

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 Dogs are Animals, as are all Cats, but Dogs are not Cats. 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 Animals acting on their animal-ness without worrying about whether they're Dogs or Cats.

更多推荐

为什么允许使用子类的构造函数实例化父类对象?

本文发布于:2023-11-04 10:11:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557647.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:子类   函数   实例   对象   化父类

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!