利用单根类层次结构 [英] Advantage of singly rooted class hierarchy

查看:223
本文介绍了利用单根类层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原谅我,如果这是一个愚蠢的问题,但我想了解在这样的.Net,Java语言的单根类(对象)层次结构的所有优点。

我能想到的一个优点,比方说,我有我想要接受所有类型的(引用)数据类型的函数。那么在这种情况下,而不是书写功能为每个数据类型,我可以写我的功能等。

 公共无效MyFun(obj对象)
{
//有些code
}
 

什么其他的优势,我们从这种类型的层次结构得到什么呢?

思考 -
解决方案

我会从一个不错的书引用一些线路在Java中通过布鲁斯·埃克尔


  

在单根的层次结构中的所有对象都有一个接口共同点,   因此它们最终都相同的类型。另一种方法(通过提供   C ++)是你不知道这一切都是相同的基本   类型。从向后兼容的角度来看这符合模型   Ç更好,可以被认为是限制较少,但是当你想   做全对面向对象编程,你必须再建立自己的   层次结构提供了内置于其他OOP一样方便   语言。在任何新的类库,你收购,其他一些   不相容接口将被使用。它需要的努力(以及可能的   多重继承)工作,新的界面到您的设计。是   额外的灵活性的C ++值得吗?如果你需要它,如果你有一个   大投入的C-这是相当有价值的。如果你从开始   从无到有,其他替代品如Java往往可以更高效。


  

在单根的层次结构中的所有对象(如Java提供)即可   被保证具有一定的功能。你知道你可以执行   在每一个特定的基本操作对象的系统。一个单向   根的层次结构,以及在堆上创建的所有对象,大大   简化参数传递。


  

一个单根的层次结构使得它更容易实现垃圾   收集器(它方便地内置到Java)。必要   支撑,可安装在基类,和垃圾收集   因此可以发送适当的消息到系统中的每个对象。   如果没有单根的层次结构和系统操作的对象   通过参考,这是很难实现的垃圾收集器。


  

自运行时类型信息是保证在所有对象,   你永远不会结束了一个对象的类型,你不能确定。   这与系统级操作,例如特别重要   异常处理,并允许在编程更大的灵活性。


Excuse me if this is a silly question, but I want to understand all the advantages of singly rooted class (object) hierarchy in language like .Net, Java.

I can think of one advantage, Let's say I have a function which I want to accept all type of (reference) data types. Then in that case instead of writing function for each data type I can write my function like.

public void MyFun(object obj)
{
// Some code
}

What all other advantages we get from such type of hierarchy?

解决方案

I'll quote some lines from a nice book - Thinking in Java by Bruce Eckel:


All objects in a singly rooted hierarchy have an interface in common, so they are all ultimately the same type. The alternative (provided by C++) is that you don’t know that everything is the same fundamental type. From a backward-compatibility standpoint this fits the model of C better and can be thought of as less restrictive, but when you want to do full-on object-oriented programming you must then build your own hierarchy to provide the same convenience that’s built into other OOP languages. And in any new class library you acquire, some other incompatible interface will be used. It requires effort (and possibly multiple inheritance) to work the new interface into your design. Is the extra "flexibility" of C++ worth it? If you need it—if you have a large investment in C—it’s quite valuable. If you’re starting from scratch, other alternatives such as Java can often be more productive.


All objects in a singly rooted hierarchy (such as Java provides) can be guaranteed to have certain functionality. You know you can perform certain basic operations on every object in your system. A singly rooted hierarchy, along with creating all objects on the heap, greatly simplifies argument passing.


A singly rooted hierarchy makes it much easier to implement a garbage collector (which is conveniently built into Java). The necessary support can be installed in the base class, and the garbage collector can thus send the appropriate messages to every object in the system. Without a singly rooted hierarchy and a system to manipulate an object via a reference, it is difficult to implement a garbage collector.


Since run-time type information is guaranteed to be in all objects, you’ll never end up with an object whose type you cannot determine. This is especially important with system level operations, such as exception handling, and to allow greater flexibility in programming.


这篇关于利用单根类层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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