您的位置:首页 > 其它

[译]OOSE第3章:面向对象知识入门 3.3 类与实例

2010-07-17 19:50 435 查看
3.3 Class and instance 类与实例
In the system we model, there will be a number of communicating objects. Some of these objects will have common characteristics and we can group the objects according to these characteristics. When we look at the objects in the example, we notice that all three people have similar behavior and information structures. These objects have the- same mold or template. Such a group represents a class. In order to describe all objects that have similar behavior and information Structures, we can therefore identity and describe a class to represent these objects.
在我们利用面向对象方法建模的软件系统中,通常会有一系列的互相通讯的对象。其中一些对象会具备公共的特征,我们可以根据这种公共特征把这些对象归类在一起。以刚才讨论的Tom, Mary,John的案例中,我们会注意到,这三个人具有类似的行为特征和内部信息结构。这些对象具有相同的铸模mold或者是模板template,这样一组对象代表的就是类。为了描述所有具有类似行为特征和信息结构的对象群体,我们可以标识identity并且描述一个类来代表这一类对象。
A class is a definition, a template or a mold to enable the creation of new objects and is, therefore, a description of the common characteristics of several objects. The objects comprising a certain class have this template in common. As an example we can view this book. The book you are holding in your hand is an instance of the book. The book description at the publishers represents the class from where instances can be created.
一个类是一个定义,一个模板或者是一个铸型,他有能力创新的对象集合,基于这一点,一个类也是针对若干对象的公共特征的一个描述。这些类是由一个具有相同模板特征的一个特定类组成,这个模板描述了这些类的共性特征。举个案例来说,我们正在看OOSE这一本书,读者手中拿着的这一本书就是书本的一个实例。关于书本的出版商的描述代表了这个类,我们可以基于这个类可以方便的创建新的实例。
“A class represents a template for several objects and describes how these objects are structured internally. Objects of the same class have the same definition both for their operations and for their information structures.”
归纳一下:“一个类代表了面向一组对象的一个模板,他详细描述了这些对象的内部构造,所有根据同一个类创造的对象在外部操作和内部信息结构上具有完全相同的定义。
A class is sometimes called the object's type. However, a type and a class are not the same thing. As we mentioned above, an abstract data type is defined by a set of operations. A type is defined by the manipulations you can do with the type. A class is more than that. You can also look inside a class, for example to see its information structure. We would therefore rather view the class as one (of possibly many) specific implementation of a type.
有时,我们也会把一个类叫做一个对象的类型type。然而,一个类型和一个类并不是同一个事物。在前面我们已经提到过,一个抽象数据类型是通过一组外部操作(operations)来定义。一个类型同样是通过外部用户所能够执行的操作(manipulation)集合来定义。然而,一个类型比抽象数据类型或者类型(type)具有更多的含义。你可以全面的观察一个类的内部结构,比如他的信息结构。所以,我们更倾向与把类定义为一个(也许很多个)面向类型的特殊实现implementation
Using the class concept, we can associate certain characteristics with a whole group of objects (see Figure 3.9). We can consider the class as being an abstraction that describes all the common characteristics of the objects forming part of the class.
通过使用类class的概念,我们可以把具有特定特征的一组对象(参考图3-9)关联在一起。我们可以认为类是一种抽象,他可以用于描述一组对象的所有公共特征,基于这种公共特征可以构成对象class的一部分。



图3.9
In object-oriented systems, each object belongs to a class. An object that belongs to a certain class is called an instance of that class. We therefore often use object and instance as synonyms.
在一个面向对象的系统中,每一个对象都会属于一个类。我们把属于一个特定类的一个对象叫做这个类的一个实例(instance)。所以我们通常把对象和实例作为同意词来使用。

An instance is an object created from a class. The class describes the (behavior and information) structure of the instance, while the current state of the instance is defined by the operations performed on the instance.
一个实例就是一个从类创造出来的一个对象。特定的类负责描述特定实例的结构(行为和信息结构),而特定实例当前的状态信息是通过面向实例执行的操作来定义的。
We can thus define a class Person, and each object that represents a person becomes an instance of this class. In our example, we can describe a class Person, where Tom, John and Mary are instances of this class (see Figure 3.10).
结合上面的案例,我们可以定义一个叫做人Person的类,这样一来每一个代表人的个体对象都成为这个类的一个实例。在这里,我们可以定义做人Person的类,那么Tom, John and Mary全部成为这个类的实例集合,(详细的描述见图3-10)



图3.10

The behavior of the instance and its information structure are thus defined by its class. Each instance also has a unique identity. Several different instances can be created from a certain class, where each instance is manipulated by the operations defined by the class. Different instances can be manipulated by different sequences of operations and, as a result, have different internal states. If these instances had been manipulated in exactly the same way, their state would also have been the same.
每一实例的行为和相关的信息结构就可以通过他属于的类来定义。为了区别不同的实例,每一个实例都拥有独一无二的标识(identity),几个不同的实例可以通过一个特定的类来创造,利用这个类定义的一组操作,外部用户可以操作或者管理manipulated每一个实例。
Different instances can be manipulated by different sequences of operations and, as a result, have different internal states. If these instances had been manipulated in exactly the same way, their state would also have been the same.
每一个实例可以执行不同的顺序的操作,那么每个实例会有不同的内部状态,假如我们针对每一个实例执行一模一样的操作,那么他们的内部状态就会一模一样。
Now, we do not need to describe how each person will appear, as this description is made in one place only, namely in the class. The class will therefore contain the information structure for age, for example, and operations to examine or modify this age. When we have created an instance (for example, John), we need to store John's age in this instance. This is achieved by using the operation for storing age.
现在,我们不需要针对每一人分别来描述他们的行为和内部信息结构,因为现在只需要在一个地方进行描述,也就是我们刚才提到的类。所以这个类需要包含必要的信息结构和行为,比如年龄,以及用来检查年龄或者修改年龄的操作。当我们利用这个类创造了一个实例,比如说John,我们需要首先保存John的年龄。这个任务是通过保存年龄的操作来实现的。
In the same way, we can make all the people's arms, legs and heads into classes. Thus we can define an individual class for head, arm, body and leg. The class for person can now use these classes to construct a person's body (see Figure 3.11).
利用同样的方法,我们还可以把所有人的组成部分,比如手,脚,头定义成新的类。具体来说,我们可以进一步面向头,手,身体,脚定义一些新的类,这样为人定义的类就可以使用这些类来构成construct一个人的身体。从图3-11可见,三个新的类,身体,头,脚。



图3.11

As all people are instances of the same class, they will have similar behavior. If we wish to describe the fact that men and women have different behavior, for example when they dance, we will need to create two classes, one for Male and one for Female. John and Tom are created as instances of the class Male, and Mary as an instance of the class Female (see Figure 3.12). We must now, for both these classes, describe their behavior and information structures.
既然所有人都是同一个类的实例,他们会拥有类似的行为。假如我们希望描述男人和女人可能会有不同的行为特征,举个例子来说,当他们跳舞时候,我们需要创造两个类,一个是男人,一个是女人。那么John and Tom是属于男人这个类创造的实例,而Mary是属于女人这个类创造的实例(see Figure 3.12).。所以现在我们必须针对每一类,描述他们的行为和信息结构。



图3.12

Why didn't we make John, Mary and Tom into separate classes instead? This is surely feasible and, if they had different behavior and/or information structures, we would have had no choice but to do so. We have simplified things by saying that all men appear in one way and all women in another. Both these classes have certain parts in common. At the moment, we must repeat these similarities, but later we shall describe how inheritance can be used to avoid this duplication of descriptions.
为什么我们不针对John, Mary and Tom创造不同的类呢,这一点非常明显,假如这三个对象具有完全不同的行为和信息结构,那么我们没有选择必须设计三个不同的类。这里,我们考虑到现实世界中所有的男人是一种行为模式,所有的女人是另外一种行为模式,所以就设计两个类来简化对象模型设计。男人和女人这两个类具有部分特点是完全相同的,此时,我们必须针对这些相同的部分进行重复的描述,在后面的章节中,我们会介绍利用继承这种概念来避免在一个面向对象的系统中重复冗余的描述类。

What is contained inside the classes Male and Female? In Figure 3.13, we can see both the information structure and the operations in each class, and we can see that they have a lot in common. The operation to create a new instance of a class (that is, create a new person) creates also legs, arms, head and body for the new person.
在男人和女人这两个类当中包含那些内部结构呢?从图3-13中我们可以观察到每一个类内部的信息结构和操作列表,同时我们发现这两个类的很多信息结构是完全相同的。举个例子来说,当我们创作类的一个实例,我们也同步创造了和这个特定人相关的手,脚,以及身体这些对象。



图3.13
To summarize, we can consider the class as being the definition of operations and information structure for objects, and the instance as defining an individual object's state.
最后总结一下,我们可以认为类是针对特定对象的操作模式和信息结构的定义,而实例则是用来定义每一个对象的当前状态。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: