[译]OOSE第3章:面向对象知识入门 3.5 Inheritance+3.6

3.5 Inheritance

When we describe our classes, it is soon noticed that many classes have common characteristics (behavior and information structure). For instance, when we compare the classes Male and Female, we see that they are very similar to each other. The similarities can be shared between the classes by extracting them and placing them in a separate class Person. In Person, we describe everything that is common to Male and Female. In this way common characteristics can be shared by several classes. We collect the common characteristics into one specific class and let the original classes inherit this class; then we need only describe the characteristics that are specific to the original classes. We therefore allow both Male and Female to inherit Person and, in this way, obtain access to all the characteristics defined there (see Figure 3.14). Male and Female now contain the same things as before, but their description has been simplified by means of inheriting Person.

  当我们在描述面向对象系统中不同的类的时候,我们很快会发现很多类既有相同的特征(行为和信息结构).举个例子来说,当我们把男人和女人这两个类进行对比,我们会发现这两个类彼此之间非常类似.假如我们能够把这两个类男人和女人这两个类之间相同的部分提炼(extract)出来并放置到一个单独的类人(Person)当中,利用这种方式两个类中相类似的部分就非常容易被互相分享.当我们定义人这个类的时候,我们可以把男人和女人这两个类中每一个相同的信息结构和行为进行完备的定义. 于这种方式,公共特性(多个类之间)可以被多个不同的类进行分享.假如我们把多个初始类的公共特性提炼出来,并放置到一个特殊的类当中,后续就可以让每个初始类来继承这个特殊的公共类,那么我们就仅仅需要针对每一个初始类来描述他们特有的信息结构和行为特性.我们可以允许男人和女人这两个类来继承人这个类, 通过这种方式, 男人和女人这两个类都能够非常方便的使用在公共类中定义的特性. (Figure 3.14)..通过继承自人Person这个公共对象, 男人和女人这两个类和以前一样拥有了相同的特征和行为;相比较于不应用继承这种方法 两个类的定义方式已经极大的简化.

  图3.14

                   图3.14

If class B inherits class A, then both the operations and the information structure described in class A will become part of class B.

假如类B是从类A继承而来, 那么所有在类A中定义的行为和信息结构都会成为类B的一部分.

By means of inheritance, we can show similarities between classes and describe these similarities in a class which other classes can inherit. Hence we can reuse common descriptions. Inheritance is therefore often promoted as a core idea for reuse in the software industry. However, although inheritance, properly used, is a very useful mechanism in many contexts including reuse, it is not a prerequisite for reuse. We shall come back to this issue in Chapter 11.

通过使用继承这种建模方法,我们可以描述不同类之间的相似之处,并能够进一步把这些公共特性定义成一个新的类, 这个新的类可以供其他的类来继承. 这就是面向对象设计中一种对公共特性的重用方法.基于这种对象重用特性,继承常常被提升为软件工作中一种核心的概念.但是从OOSE的观点来看,虽然合理的使用继承是一种非常有用的机制在很多软件开发的上下文场景下,包括软件复用(reuse),但是其实继承并非软件复用的先决条件,我们会继续讨论这个问题在第11.

Through inheritance, we have also obtained another advantage. If we want to modify some characteristic in Person (for example, how a person walks), it is sufficient to perform this modification in one place. Thus, if such a modification is made, both Male and Female will inherit this new definition of walk. Hence inheritance is very useful for easy modification of models.

  通过应用继承这种软件建模技术,我们也获得了其他的优势.假如我们希望变更人的某些公共特性(举个例子来说,一个人是如何走路),我们可以非常方便的在一个地方进行这种调整.如果我们实施这种调整以后,无论是男人和女人这两个类都能够继承这种关于行走这一行为的更新定义.因此,继承是一个非常有用的,容易操作的建模技术来实现对模型的快速变更.

We also see from the examples that the descriptions of Male and Female have been greatly reduced. The only information remaining is that which differs between them. Inheritance is thus also useful for avoiding redundancy, leading to smaller models that are easier to understand.

从上述的案例中,我们还会发现关于男人和女人这两个类的描述被极大的精简了. 我们只需要描述那些两者存在差异的行为和信息结构. 所以,继承这种技术也能够非常有效的减少信息冗余, 进而设计出更加小巧,更加容易理解的模型.

The ease of modification does not only occur inside class descriptions. Adding new classes can be easily done by just describing changes to existing classes. However, adding new classes may sometimes involve restructuring the inheritance hierarchy. We shall come back to this issue later.

 使用继承这种方式不仅仅可以简化修改类的内部信息结构这一工作任务.当我们需要添加一个新的类的时候,我们只需要描述新创建的类和已知的类之间有那些区别,所以这种添加工作会变得很简单.在现实的面向对象的建模过程中,增加新的类也有可能会引发对已知道的继承关系层次机构的调整与重构. 我们在后面会回来讨论这个问题.

By means of extracting and sharing common characteristics, we can generalize classes and place them higher up in an inheritance hierarchy. In the same way, if we wish to add a new class, we can find a class that already offers some of the operations and information structure required for the new class. We can then let the new class inherit this class and only add anything which is unique for the new class. We then specialize the class.

通过提炼和共享不同类之间的公共特征,我们可以针对对已知的类进行泛化(generalize)处理,并把相关的公共部分提升到一个继承的层次结构的高层次部分中. 通过这种方法,假如我们希望添加新的类,我们可以首先分析那些已知的类已经具有新的类需要的部分操作和信息结构. 后续我们可以让新的类继承于这个已经存在的最合适的类, 并针对新增加类的特有部分单独进行定义. 这种利用继承已知类来创建新类的建模方式方法称为类的专门化(specialize)

Classes lying below a class in the inheritance hierarchy are called descendants of the class. Classes lying above are called ancestors. Sometimes the concepts of subclasses and superclasses are used instead, but we prefer descendant and ancestor. The reason for this is discussed in the box on super- and subclasses. Hence, if class B inherits from class A, then class A becomes class B's ancestor (see Figure 3.15). Class B is then a descendant of class A. Since inheritance hierarchies may include several classes, we may want to emphasize the relation between two classes. If a class directly inherits from another class, we call it a direct descendant. The first class is then the direct ancestor of the second class. A direct ancestor is sometimes called the parent and a direct descendant is sometimes called the child.

 

继承是一种层次化的结构,在继承结构中一个特定类下层的所有的类,全部被称为这个类的后裔descendants. 而一个特定类上层的类被叫做祖先ancestors. 有时我们也用子类和超类的概念来代替, 但是我们更加倾向于使用后裔descendants和祖先ancestors的概念.这个原因我们会在补充阅读材料中描述. 归纳一下如果类B是继承于类A,那么类A就是类B的祖先. (see Figure 3.15)而类B就是类A的后裔.既然继承层次结构可能会包含多个类,我们可能需要强调两个类之间的关系.如果一个类是直接继承与另外一个类,我们称为直接后裔direct descendant. 第一个类就是第二个类的直接祖先(direct ancestor). 一个直接祖先类有时候也叫做父母类,而一个直接后裔类有时候也叫做子女类

图3.15

             图3.15 

In this book, inheritance relations are indicated by a dashed arrow drawn from the descendant towards the ancestor class. We will also write 'inherits' (or shorter, 'ihs') on the arrow to avoid any misunderstandings. Note that the direction of the arrow is from the descendant towards the ancestor. In some other notations the direction is the opposite. The reason that we use this convention is that the descendant should know about its ancestor, but the ancestor should not know about its descendant.

OOSE这一本书中,继承关系是用一个虚线的箭头来标识的,箭头的方向是从后裔类descendants指向做祖先类ancestors.另外我们会在箭头旁边标识一个'inherits' (or shorter, 'ihs')来避免歧义.注意箭头的方向是从从后裔类descendants指向做祖先类ancestors.在有些标识系统中,这种方向是相反的.我们应用这种箭头模式的原因是在于,我们认为后裔类descendants必须知道他们的祖先类ancestors,而祖先类ancestors则不需要知道他们的后裔类descendants.

The inherit association is a class association, that is, an association between classes. Class associations are drawn with dashed arrows here. Associations between instances are drawn with a full arrow.

继承关系是一种类之间的关系,也就是说,类与类之间的静态关联关系. 在这里类之间的关联关系用虚线来表示,而实例之间的关联关系是通过一个实线来标识.

Ancestors developed with the main purpose of being inherited by others are often called abstract classes. Usually, instances are not created from abstract classes, although it is possible. A class developed with the main purpose of creating instances of it is called a concrete class. Also, concrete classes may of course be ancestors of other classes.

OOSE的建模方法中,祖先类(ancestors)构造的目的是方便其他类继承应用, 祖先类(ancestors)有时候也称为抽象类(abstract classes).在实际应用中通常抽象类不能直接用于创建实例,虽然这在理论上是有可能的.而有些类设计的目标主要是用来直接创造实例的类,这些类被称为实体类(a concrete class).当然一个实体类也完全有可能成为其他类的祖先类.

 

3.5.1 Using inheritance

    应用继承

An inheritance hierarchy can consist of many levels; that is, classes can be inherited from other classes which, in turn, can be inherited from new classes, and so on. How then should you use inheritance to get a good and robust inheritance hierarchy? We will discuss here some topics from our experience to explain how to get a good inheritance structure.

 在面向对象的系统中,一个层次化的继承模型可能会包含很多层次,在这种情况下,一些类可以从其他一些祖先类继承而来,同时他们也会有自己的后裔类,并可以持续的继承下去. 那么在具体的面向对象的建模过程中,你如何能够获得一个结构清晰,具有性能稳定的层次化继承结构呢? 关于如何设计良好的继承结构这个问题,我们会基于应用OOSE方式的项目实践中积累的一些经验在此讨论几个主题.

Class structuring occurs with the help of inheritance hierarchies. This structuring of classes in an inheritance hierarchy enables us to work with classes and to define new classes by specifying the differences between the new ones and the already existing ones. If this expansion becomes extensive, then the inheritance hierarchy may become less suitable and a restructuring may be necessary.

借助层次化的继承结构这种设计方法, 可以非常方便的构建层次话的类的结构. 依托层次化的继承结构这种方法, 我们可以基于已经有的类来设计新的类, 在设计的过程中仅仅只需要定义新的类和已经有的类之间存在的差别.这种扩展的过程中具有继承关系的一组类的特性范围变得非常广泛,那么这种层次话的继承关系可能变得不适合, 此时就必须进行合理的重构.

For instance, what happens if we add two further individuals to the example, James and Lawrence? They are both men and can therefore be instances of Male. Such an addition is trivial, but what if we want to describe them as old men having a few characteristics differing from those of their younger colleagues? For instance, they cannot dance in the same way as the younger men. Their dance style is a little more rigid. To describe the older men, we can add a new class: Old Male. How will this new class be related to the existing ones? It is clear that the class Old Male has all the characteristics of the class Person. It also has a lot in common with Male. Basically we have three relevant possibilities (see Figure 3.16):

举个例子来说,假如我们想另外增加2个个体James Lawrence但前面举的案例中会发生什么样的情况?因为James Lawrence都是男人,因此非常容易考虑为设计为类男人的实例,这增量定义是很直接和简单的, 但假如我们想定义他们具有和年青男人不同的行为特性的老年人该如何办呢?:举个例子来说,他们不能和年轻的同事一样的舞蹈类型,他们只会跳比较传统的舞蹈. 为了描述老年人的行为特点,我们可以考虑增加一个新的类:老年男人. 那么这个新类和已经存在的男人和女人这两个类是什么关系呢?此外,老年男人和男人之间还是存在很多的相同的特点。直观的来分析,我们有三种可能性(see Figure 3.16)

 

(1) Old Male is a descendant of Person and we define the differences between Old Male and Person.

(2) Old Male inherits Male and we define the differences; that is we redefine dance.

图3.16

                         图3.16

(3) We create a further class, Young Male, and define dance there as well. In this way, both Old Male and Young Male can inherit Male.

1)类“老年男人”是类“人”的后裔,我们只需要定义两者之间的差别。

2)类“老年男人”是类“男人”的后裔,我们只需要定义两者之间的差别。换句话说,重新定义(redefine)跳舞(dance)这个行为。

 (3) 我们创建一个新的类“年青男人”,并对这个类也定义相关的跳舞(dance)行为,通过这种方式,类“老年男人”和类“年青男人”都可以继承于类“男人”。

Which of these three alternatives should be selected? This will depend on how the classes are to be used. How will further modifications be handled? What do we really want to model? How important is a clear understanding? How difficult is it to restructure the classes? What is the price of restructuring? The best alternative will depend on how you answer these questions.

在具体的建模过程中,具体应该在三种方式中选择那一种呢?这种选择取决于如下的几个要素:

A)在模型中这些类该如何应用?

B)在后续的建模中会面临那些变化?

C)那些类的行为特点是我们希望来建模的?

D)模型的易于理解性。

E) 对类进行重构的难度?

F)重构所消耗的人力成本。

最好的选择取决与设计者如何均衡以上的这6点要素。

Ideally, for the purposes of understanding, we would select the third alternative: to create a further class, Young Male, extract the similarities and place these in class Male. With this, we now have no redefinition, and we keep a clear and understandable structure that can be further developed. The cost has been the work involved in restructuring the existing class structure and creating the new class.

在理想的情况下,第三种设计方法是最容易理解的,创建一个新的类,年青男人,挖掘他和类“年老男人”之间相同的特点,并设计在类“男人”中。基于这种方法,我们没有对已经存在类的操作进行重新定义(redefinition),同时我们保持了一个清晰并且是易于理解的信息结构,方便后续的系统重构与扩展。需要付出的代价主要是重新设计已经有的类,并创建新的类。

When a new class is to be added, we try to find a suitable candidate to be the direct ancestor. As we have seen, some operations may need to be modified for the inheritance. Normally we have four possibilities for adding a new class:

OOSE的建模过程中,如果需要添加一个新的类,我们总是希望寻找直接祖先类(direct ancestor)为合适的候选对象。正如前面所看到的,在继承结构中的一些操作需要被更新。通常我们有四种选择。

(1)  We can go upwards in the inheritance hierarchy to see whether an ancestor is more suited for inheritance (the characteristics that we wish to avoid may not exist in the ancestor).

(2)  We can describe the class from the beginning, independent of any other class (that is, without inheritance).

(3)  We can restructure the inheritance hierarchy, so that we obtain a class that suits the inheritance required; see Figure 3.17 for an example. However, this is not always possible.

(4)   We can redefine the characteristics that we wish to change.

(1)  我们可以在现存的继承结构中向上寻找,选择一个合适的祖先类来适合新的类来继承。(原则是新设计类不需要的一系列特性可能在祖先类中也不存在,这样就可以很容易来继承和扩展)

(2)  我们可以独立的新定义一个新的类,和已知的类没有任何关系(换句话说,不应用继承结构)

(3)  我们可以对现存的继承结构进行重新设计,这样一来我们可能可以获得一个合适的类来满足继承结构的要求。see Figure 3.17作为一个案例。虽然很多时候,这在实际中不一定可行。

(4)   我们可以针对一些特性根据设计的需要进行重新定义(redefine)。

 

The first two solutions are trivial and we shall not discuss them any further. The third solution is the most acceptable, as it maintains a clean class hierarchy. To restructure in this way, though, often requires a lot of work, partly owing to the work involved in finding a better structure and partly owing to the consequences that the modification will have on a system designed on the basis of an already existing class hierarchy. This solution is usually not as easy as shown in the figure and thus compromises may need to be made in order to find where the operations can be defined most suitably.

.前面的两种解决方案非常简单和直观,所以我们不会进一步讨论。第三种解决方案是最可以接受的,因为他保持了一个非常清晰的继承关系。采用这种重建方式通常会需要大量的工作,部分原因在于设计一个合适的结构,部分原因在于后续根据新的结构对基于现存类结构的系统的修改。这种解决方案通常不会象图3.17那样描述的那么简单,这样在重构时就必须详细的设计和比较,在继承层次结构的那一个层级上定义是最合适的。

The fourth solution is often called overriding. It means that we redefine some behavior and/or some information structure from an ancestor. Whether overriding should be used is widely discussed in the object-oriented community. Overriding is both easy and flexible to use for modifying existing classes, but it can damage understanding of a class hierarchy, as operations with the same name can have different semantic meanings in different classes. An inheritance chain must then be followed upwards to the place where the operation is defined to find its definition. After the inheritance has passed from one class to another, the inherited characteristics can be modified. With override, therefore, inheritance is not transitive. Thus the descendant has not inherited all the ancestor's characteristics; only some ol them have been inherited, while others have been redefined.

第四种方案有时也叫做覆盖忽略(overriding)。这就意味着我们会从祖先类中继承的一些行为和信息结构进行重新定义。在面向对象的社区中,是否需要应用覆盖忽略(overriding)这种设计模式在面向对象的社区中被广泛的讨论。覆盖忽略(overriding)这种设计方式在对现有的类进行重构的场景下同时具备了简单和灵活的特点,但是这样一来也破坏了类的结构关系的可理解性,因为我们会发现相同名称的操作在不同的类当中可能会有不同的操作定义方式(semantic meanings。在一个层次化的继承链条中必须向上搜索到合适的位置来发现操作的定义方法,这是一种理想的定义方法。当这种继承关系从一个类传递到另外一个类,一些继承的特性可能被修改。通过应用覆盖忽略(overriding),这样继承关系就可能会发生改变。这样一来后代类descendant有可能仅仅继承了祖先类的部分特点,一些被继承,一些被重新定义。

图3.17

                                 图3.17 

To understand better how we should use inheritance in a proper way we shall review the main purposes of inheritance.

为了更好的理解我们该如何应用继承这种建模的方法,我们需要再次回顾一下在面向对象方法中应用继承这种设计方法的主要目的。

Reuse. The most common reason for using inheritance is that it simplifies the reuse of code. Reuse can, in principle, occur in two different ways in combination with inheritance. The first is that two classes are found to have similar parts; these parts are extracted and placed in an abstract class, which both the original classes inherit. This abstract class represents the common parts of both classes and need not always be meaningful in itself. The other way to reuse is to start from a class library. Find a class which has the operations that you need, inherit this class and make the required modifications.

重用Reuse.:在面向对象的设计方法中使用继承最重要的一个理由是这种方式简化了代码重用的过程。从核心原理上说,使用继承这种设计方法在两个层面可以极大的简化软件重用的过程。首先,我们从两个类当中提炼相类似的公共部分,这些公共部分被抽取出来并放置在一个抽象类abstract class当中,这样两个实体类可以直接继承抽象类。这个抽象类代表了两个类的公共部分,因此对他自身来说不一定总是需要有含义。另外一种重用的方式是从一个类库开始设计。发现一个类能够提供你所需要的操作,新设计一个类来继承已知的类库中的类,并进行必须的补充定义,通过这种方式来简化代码设计的过程。

 

Subtyping. A class can be regarded as an implementation of a type. A class A defines a certain behavior. If it is possible to use one of A's descendants in all the places where class A is used, then we say that the classes are behaviorally compatible. The descendant represents a subtype to class A. In practice this often means that the descendant should at least have the same interface as its ancestors. We can say that the ancestor here represents a subset of the behavior common to all the descendants. Subtyping normally occurs if the inheritance performs only an extension, rather than overriding something already defined in the ancestor. The additions must be disjunct with the existing classes, and therefore must not create any restrictions on them. This use of inheritance is tightly coupled to describing the role played by an object; the role is defined by the operations of the object. To think in terms of roles or responsibilities is often appropriate for modeling object-oriented systems and provides a good tool.

Subtyping. A class can be regarded as an implementation of a type. A class A defines a certain behavior. If it is possible to use one of A's descendants in all the places where class A is used, then we say that the classes are behaviorally compatible. The descendant represents a subtype to class A. In practice this often means that the descendant should at least have the same interface as its ancestors. We can say that the ancestor here represents a subset of the behavior common to all the descendants.

子类型(Subtyping.)我们通常可以把一个类理解为一个类型(a type)的实现(an implementation)。举个例子来说类A定义了一个特定的行为,假如在类A应用的地方,我们可以使用类A的所有的后裔类其中的一个,我们就认为这些类是行为兼容behaviorally compatible.的。这些所有的后裔类代表了类A的一个子类型(Subtyping.)。在真实的软件设计中,着就意味着后裔类至少需要和他们的祖先类具有相同的接口,We can say that the ancestor here represents a subset of the behavior common to all the descendants.我们就可以说,在这种场合下祖先类代表了所有后裔类相同的行为接口子集(a subset)。This use of inheritance is tightly coupled to describing the role played by an object; the role is defined by the operations of the object. To think in terms of roles or responsibilities is often appropriate for modeling object-oriented systems and provides a good tool. 子类型(Subtyping.)这种设计方式主要应用场景是在我们期望后裔类是已知类的一个扩展extension,而不是覆盖(overriding)已知祖先类的某些行为特性。那么后裔类的增量部分的行为特性必须和已知的祖先类不相同(disjunct),所以不能对这些增量部分进行特殊的限制。这种针对继承的应用模式与达成准确描述对象所能够执行的行为规则这一目标有着密切的联系,因为对象的行为规则是通过相关操作来规定的。关注对象的行为规则(roles)或者责任(responsibilities)是面向对象的建模过程中非常正确的一种思维方式,同时也能够提供非常好的建模工具.

 

Specialization. If the descendant is modified in such a way that it is no longer behaviorally compatible with its parent (that is, if the parent class can no longer be exchanged with the descendant), the class is said to be specialized. Normally, the operations and/or information structure have been redefined or deleted. A parent class that has been specialized cannot always be replaced by its descendant. An example of specialization is if a class Adult inherits from another class Person (see Figure 3.18). The class Adult has a more restricted age interval than class Person. We cannot, therefore, arbitrarily replace the class Adult with the class Person. Consider the case when one wishes to enter a person's age as 5. This is not possible for instances of the class Adult, but is possible for instances of the class Person.

特例化Specialization: 假如我们对后裔类进行了某种修改,适得继承结构中的后裔类和祖先类无法行为兼容,(换句话说父亲类无法和子女类进行互换). 那么后裔类就被认为是特例化. 通常情况下,相关的操作,以及信息结构已经被重新定义或者被删除. 一个已经被特例化的父亲类通常无法被他特例化的子女类来置换. 我们可以举一个例子是类职员和类的例子(3.18可见) . 因为类职具有特殊的年龄限制,所以我们不能用类来代替类职员.考虑这样的场景,如果我们希望定义一个人的年龄是5, 那么这对于职员类是不可能的,而对类是有可能的.

图3.18

                            图3.18

Conceptual. This use of inheritance corresponds closely to the intuitive semantic, 'a dog is a mammal' (that is, in all places where we use mammal, we can also use dog, as dog maintains all the characteristics of mammal).

概念Conceptual. 使用继承和直觉含义有密切的关系(intuitive semantic) ,比如说狗是动物,(换句话说,在所有需要使用动物的地方,我们也可以使用狗,因为狗具有所有动物需要的特性)

   These different ways of using inheritance are not exclusive or disjunct in any way. For example, reuse can be a reason for subtyping. In order to optimize the use of inheritance and be able to recognize both good and bad solutions, more experience must be gained and more research done on the use of inheritance. However, from our experience, focusing on an object's protocol normally leads to appropriate inheritance structures. This means that focusing on the subtyping issue at all times will lead to proper inheritance hierachies that are maintainable and robust. This usage is also natural from a logical perspective. What should be avoided is the use of inheritance for reuse of single operations; that is, even if you can only reuse a single operation, you inherit the class anyway. Although this may be proper and efficient for prototyping purposes, such use of inheritance often leads to code that is not maintainable or robust in the long run. We may talk about this type of use as spaghetti inheritance.

The use of inheritance is discussed by, for example, LaLonde and Pugh (1991b), Wegner and Zdonik (1988) and LaLonde, Thomas and Pugh (1986).

.以上几种应用继承的模式并不是互相排斥或者是没有联系的.举个例子来说,重用可能是应用子类型的一个理由.为了更好的优化继承的使用,并能够识别那些是好的解决方案,那些是不好的解决方案, 一个OOSE的设计者必须通过练习积累更多的经验,并在继承的应用上积累更多的经验. 然而根据我们的经验,关注对象的接口协议通常会引导出合适的继承结构. 换句话说,在所有的时间记得关注子类型(subtyping)这个应用思路,将引导出合适的继承结构,这种继承结构不但易于维护maintainable,而且非常稳定robust. What should be avoided is the use of inheritance for reuse of single operations; that is, even if you can only reuse a single operation, you inherit the class anyway. Although this may be proper and efficient for prototyping purposes, such use of inheritance often leads to code that is not maintainable or robust in the long run. We may talk about this type of use as spaghetti inheritance. 子类型(subtyping)这种应用方法也具有逻辑概念,因此非常容易理解. 在继承这种应用场景中,关于单一操作集合single operations的重用是应该避免的,具体来说,即便是你可以继承一个单一操作(a single operation),你使用了一种不合适的继承方式.虽然在基于模型(prototyping)构建这种目的的场景下,这种应用方法是合适,或者充分,但是这种关于继承的应用方法会导致代码在后续的扩展中的维护困难,以及不稳定.我们可以把这种继承称为意大利面条方式的继承(spaghetti inheritance.)

3.6 Summary

3.6 总结

Object-orientation is a technique for developing models. It maps very well the way people think about reality. Therefore object-oriented models are often easy to understand. Additionally, modifications are often local since changes frequently evolve from some individual item that is modeled.

  面向对象的设计是一种构建软件系统模型的技巧. 他能够把人们认知客观事件的方式非常完美的映射到对象模型中,因此,面向对象的模型通常非常容易理解.另外,针对面向对象系统的重构通常是本地化的,因为很多变化发生在模型中的一些单个条目individual item.

An object is a representation of some entity with both behavior and information tied to it. Only the operations that can be performed on the object are seen from the outside of the object. Objects are related and the overall dynamics of the model arise when the objects start to interact by sending stimuli to each other. However, supporting objects is not in itself object-orientation.

一个对象是一些同时具有行为和信息结构绑定关系的实体entity .如果我们从外部世界的角度来观察一个对象,我们仅仅能够看到对象的操作. 不同的对象互相关联,通过激励这种方式互相通讯,当不同的对象开始互相通信,我们可以观察到整个面向对象系统的行为特征.然而,支持对象的定义并不是面向对象这种建模方法本身.

Many objects may be similar. A class describes the similarities of objects. Every object is then an instance of the class and the behavior and information structure of the instance are defined by its class. To make the description of classes more flexible, polymorphism is used. This means that, when defining relations between objects, one object does not need to know the exact class of the other object. In this way, new classes can be introduced without needing to change other classes.

很多对象可能是类似的. 一个类可以来描述不同类的相似之处. 每一个对象都是特定类的一个实例,并且他的行为和信息结构由他归属的类来定义. 基于类与类之间的描述更加灵活flexible这个目的, 我们使用多态这种方法.这就意味着,当我们定义类与类之间动态关系的时候,一个对象不需要知道和他通讯对象所归属的特定类.通过这种方法,新的类可以方便的加入而不需要改变已经存在的类.

Classes can be described as changes to existing classes with inheritance. This makes the definition of new classes easy as only differences need to be described. However, although inheritance is a very strong tool when properly used, when improperly used it can lead to models that are hard to understand and maintain. Multiple inheritance is a much stronger tool than single inheritance. Improperly used, though, multiple inheritance can be very much worse than single inheritance.

  应用继承这种方法,我们可以通过描述新的类和已经知类的区别的方法来定义新的类,这样就使得新类的定义方法过程非常简单. 虽然在正确的应用的情况下,继承是一种非常强大的方法,而不合理的应用会导致模型难以理解,难以重构和维护.多重继承相比较于单继承而言是一个更加强大的工具;然而在错误的应用场景下,会导致更加严重的质量问题.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值