Specifying Object Implementations 指定对象的实现


So far we've said little about how we actually define an object. An object's implementation is defined by its class. The class specifies the object's internal data and representation and defines the operations the object can perform.

到目前为止我们了解了一些实际定义的对象。对象的实现是被它的类定义的。该类定义了对象的内部数据、特征和定义了对象可以实现的运算。

Our OMT-based notation (summarized in Appendix B) depicts a class as a rectangle with the class name in bold. Operations appear in normal type below the class name. Any data that the class defines comes after the operations. Lines separate he class name from the operations and the operations from the data:

我们的模型描述了类,在矩形中类名是粗体字。在类名的下面用正常的类型显示了运算。在运算的后面定义了类含有的任何数据。 横线将类名、运算和数据分开了。

Return types and instance variable type sare optional, since we don't assume a statically typed implementation language.

自从我们不再假定静态类型实现语言,返回类型和实际变量类型是可选择的

Objects are created by instantiating a class. The object is said to be an instance of the class. The process of instantiating a class allocates storage for the object's internal data (made up of instance variables) and associates the operations with these data. Many similar instances of an object can be created by instantiating a class.

对象通过类的实例化创建。这个对象成为该类的一个实例。实例化类的进程为类的内部数据(有实例化的变量组成)分配储存空间并且支持了有关这些数据的运算。通过实例化类很多相似的实例化对象被创建。

A dashed arrowhead line indicates a class that instantiates objects of another class. The arrow points to the class of the instantiated objects.

虚线箭头指向了类,该类是另一个类实例化的对象。箭头指向实例化对象的类。

New classes can be defined in terms of existing classes using class inheritance. When a subclass inherits from a parent class, it includes the definitions of all the data and operations that the parent class defines. Objects that are instances of the subclass will contain all data defined by the subclass and its parent classes, and they'll be able to perform alloperations defined by this subclass and its parents. We indicate the subclass relationship with a vertical line and a triangle:

新类可以用已经存在的类用继承来定义。当子类由父类继承而来时,子类包含了父类所有定义的数据和运算。子类对象的实例化包含子类和父类定义的数据,和他们有能力执行的由父类和子类定义的对象。我们用垂直线和三角形指明了子类和父类之间的关系。

An abstract class is one whose main purposeis to define a common interface for its subclasses. An abstract class willdefer some or all of its implementation to operations defined in subclasses; hencean abstract class cannot be instantiated.

The operations that an abstract class declares but doesn't implement are called abstract operations. Classes that aren't abstract are called concrete classes.

抽象类的主要目的之一是为它的的子类定义了普遍的接口。抽象类会定义在它的子类中一部分或者全部要实现的运算。因此抽象类不能被实例化。抽象类声明了运算但是没有实现这些运算。类不是抽象为具体的类。

Subclasses can refine and redefine behaviors of their parent classes. More specifically, a class may override an operation defined by its parent class. Overriding gives subclasses a chance to handle requests instead of their parent classes. Class inheritance lets you define classes simply by extending other classes, making it easy to define families of objects having related functionality.

子类可以定义和重定义父类的行为。更具体的说,类可以覆盖它的父类定义的预算。覆盖给子类而不是父类一个机会来把握命令。类的继承可以让你用已存在的其他类简单的定义新类。更容易用用相关的函数来定义家族式的对象。

The names of abstract classes appear inslanted type 倾斜式todistinguish them from concrete classes. Slanted type is also usedto denote abstract operations. A diagram may include pseudocode for anoperation's implementation; if so, the code

will appear in a dog-eared box connected by a dashed line to the operation it implements.

抽象类的名字是倾斜的,是为了更容易和具体类区分。抽象类型也用来表示抽象运算。图包含了运算实现的伪代码。如果是这样的话,代码会显示卷角的方框内,虚线指向了运算的实现。

A mixin class is a class that's intended toprovide an optional interface or functionality to other classes. It'ssimilar to an abstract class in that it's not intended to be instantiated. Mixinclasses require multiple inheritance:

混合类是一个为其他的类提供指向接口或函数的类。它和抽象类是相似的,它没有被实例化的打算。混合类要求复杂的继承。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值