子类和继承的类之间是否有技术上的区别? [英] Is there any technical difference between a subclass and an inherited class?

查看:83
本文介绍了子类和继承的类之间是否有技术上的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从另一个类继承的类和作为其子类的类之间是否存在技术上的区别? 以下代码中的A和B有什么区别:

Is there any technical difference between a class which inherits from another class and a class which is a subclass of it? What's the difference between A and B in the following code:

A)

public class foo {
 ...
 private class bar {...}
}

B)

public class foo { ...}

private class bar extends foo {...}

推荐答案

在继承的情况下,您可以将派生类视为其基类.也就是说,您可以保留一组基类引用,并调用它们的方法,而无需知道实际对象是否为任何派生类型,如果是,则为哪种类型.这是一个重要的语义概念.

In the inheriting situation, you can treat the derived class like its base class. That is, you can keep a collection of base class references around and call their methods without needing to know whether the actual objects are of any derived type, and if yes which. This an important semantic concept.

在成员类的情况下,内部类只是外部类的实现细节,甚至对消费者也不可见.即使它是可见的,它也只是foo类的功能之一.您会发现这两种情况在语义上有很大的不同,您必须选择一种最能代表您实际数据模型的情况.

In the member class case, the inner class is just an implementation detail of the outer class and not even visible to the consumer. Even it it were visible, it would just be one feature of the foo class. You can see that the two situations are semantically very different, and you have to pick the one which represents your actual data model most accurately.

从技术上来说,由于各种虚函数调用,派生类可能会重一些,但是具体取决于您的语言和具体情况.

Technically, the derived class probably weighs a tiny bit heavier because of various virtual function calls, but the details depend on your language and the concrete situation.

这篇关于子类和继承的类之间是否有技术上的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆