UML基本架构建模--关联的通用建模技术(二)



Modeling Single Inheritance

建模简单的继承

 

In modeling the vocabulary of your system, you will often run across classes that are structurally or behaviorally similar to others. You could model each of these as distinct and unrelated abstractions. A better way would be to extract any common structural and behavioral features and place them in more-general classes from which the specialized ones inherit.

在构建你系统的词汇表时,你经常会遇到结构上或是行为上与其它类相似的类.你可以将它们每一个作为不同的和不相关的抽象事物建模.一个更好的方法是提取任何共同的结构和行为特征并将其放在更一般的类,利于专有类继承.

 

To model inheritance relationships,

lGiven a set of classes, look for responsibilities, attributes, and operations that are common to two or more classes.

lElevate these common responsibilities, attributes, and operations to a more general class. If necessary, create a new class to which you can assign these elements (but be careful about introducing too many levels).

lSpecify that the more-specific classes inherit from the more-general class by placing a generalization relationship that is drawn from each specialized class to its more-general parent.

建模继承关系,

l给定一组类.寻找两个或更多的类共同的职责,属性和操作.

l提升这些共同的职责,属性和操作到更一般的类.如果有必要,为这些元素创建一个新的类(但是要小心引入层次过多).

l指定更具体的类继承自更一般的类是通过放置泛化来描述每个专有类和它的更一般的父类之间的关系.

 

For example, Figure 5-10 shows a set of classes drawn from a trading application. You will find a generalization relationship from four classes----CashAccount, Stock, Bond, and Property----to the more-general class named Security. Security is the parent, and CashAccount, Stock, Bond, and Property are all children. Each of these specialized classes is a kind of Security . Youll notice that Security includes two operations: presentValue and history. Because Security is their parent, CashAccount, Stock, Bond, and Property all inherit these two operations, and for that matter, any other attributes and operations of Security that may be elided in this figure.

例如,在图5-10中显示的来自交易应用的一组类.你将会找到的泛化关系:从四个类--现金账户股票债券地产--到更一般的名字为安全的类.安全是父类,现金账户股票债券地产都是子类.这些专有类的每一个都是安全的一个类别.你还会注意到安全包含两个操作:当前值历史.因为安全是它们的父类,所以现金账户股票债券地产都继承了这两个操作,因为重要性的原因,在这个图中省略了安全的其它属性和操作.

 

You may notice that the names Security and presentValue are written a bit differently than others. Theres a reason for this. When you build hierarchies as in Figure 5-10, you often encounter nonleaf classes that are incomplete or are simply ones for which you dont want there to be any objects. Such classes are called abstract . You can specify a class as abstract in the UML by writing its name in italics, such as for the class Security. This convention applies to operations such presentValue and means that the given operation provides a signature but is otherwise incomplete, so it must be implemented by some method at a lower level of abstraction. In fact, as the figure shows, all four of the immediate children of Security are concrete (meaning that they are nonabstract) and must each provide a concrete implementation of the operation presentValue.

你可能注意到安全当前值这两个名称的书写方式与其它的名称有点不同,这样做是有原因的.当你构建如图5-10这样的层次体系时,经常会遇到有子节点的类,它们并不完整或是因为你不想有任何对象而被简化.像这样的类被称为抽象.你可以在UML中通过将它的名称写为斜体来描述一个抽象类,如安全类.这个约定适用于当前值这样的操作,意思是给定的操作提供一段描述但不完整,所以在抽象的较低层必须通过一些方法来实现.事实上,如图显示的,安全的四个直接子类是具体的(就是说它们不是抽象的),而且都必须提供当前值操作的具体实现.

.

Your generalization/specialization hierarchies dont have to be limited to only two levels. In fact, as Figure 5-10 shows, it is common to have more than two layers of inheritance. SmallCapStock and LargeCapStock are both children of Stock, which, in turn, is a child of Security. Security is therefore a root class because it has no parents. SmallCapStock and LargeCapStock are both leaf classes because they have no children. Stock has a parent as well as children, so it is neither a root nor a leaf class.

你的泛化/专有层次结构不应该仅限于两个层次.事实上,象图5-10显示的,通常有超过两层的继承.小盘股大盘股都是股票的子类,而股票安全的一个子类.安全因为没有父类所以它是一个根类.小盘股大盘股因为没有子类而被称为叶类.股票有一个父类又有子类,所以它即不是根类也不是叶类.

 

Although it is not shown here, you can also create classes that have more than one parent. This is called multiple inheritance and means that the given class has all the attributes, operations, and associations of all its parents.

尽管在这里没有显示,你仍然可以创建有超过一个父类的类.这被称为多重继承,意思是给定的类有它所有父类的所有属性,操作和联合关系.

 

Of course, there can be no cycles in an inheritance lattice; a given class cannot be its own ancestor.

当然,在一个继承的框架里是不能循环的;一个给定的类不能继承它自己.

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值