gpt4 book ai didi

oop - UML 类图中的关系如何继承?

转载 作者:行者123 更新时间:2023-12-04 11:33:13 25 4
gpt4 key购买 nike

我想知道关联、依赖和此类关系如何在 UML 中继承(或者说,一般来说)。所以,在这样的情况下:

  ┌──────────┐                                        ┌──────────┐
│ ClassA │ │ ClassB │
├──────────┤ ├──────────┤
│ │─────────"One kind of relation"────────>│ │
├──────────┤ ├──────────┤
│ │ │ │
└──────────┘ └──────────┘
^
/┬\




┌─────┴────┐
│ ClassC │
├──────────┤
│ │
├──────────┤
│ │
└──────────┘

注意:

  • ClassA-ClassC是泛化关系,箭头是实心
  • ClassA-ClassB 属于[依赖、关联、聚合、组合]之一
  • Unicode 很酷,但使用编辑器字体看起来更好:)

我的问题是,这些关系是如何继承的?比如ClassA依赖ClassB,ClassC会不会依赖ClassB?等等

谢谢。

最佳答案

简单的答案是肯定的(出于最实际的目的,您不需要超出这个范围)。

但事情比看起来要复杂得多;引用统一建模语言引用手册,第二版:

The words generalization and inheritance are often used interchangeably, but there are actually two related but distinct concepts. Generalization is a taxonomic relationship among modeling elements. It describes what an element is. Inheritance is a mechanism for combining shared incremental descriptions to form a full description of an element. In most object-oriented systems, inheritance is based on generalization, but inheritance can be based on other concepts, such as the delegation pointer of the Self language. Basing the inheritance mechanism on the generalization relationship enables factoring and sharing of descriptions and polymorphic behavior. This is the approach taken by most object-oriented languages and by UML. But keep in mind that there are other approaches that could have been taken and that are used by some programming languages.

我记得早在 2003 年就泛化和继承之间的区别进行了一次相当长的演讲。简而言之,这两个概念属于软件设计的不同层次,还是引用 Martin Fowler 在 UML Distilled, Third Edition 中的“建模的各种视角”:

Conceptually, we can say that a Corporate Customer is a subtype of Customer if all instances of Corporate Customer are also, by definition, instances of Customer. A Corporate Customer is then a special kind of Customer.

泛化概念属于概念、设计层面。

但是继承是一个属于实现角度的概念:

With a software perspective, the obvious interpretation is inheritance: The Corporate Customer is a subclass of Customer. In mainstream OO languages, the subclass inherits all the features of the superclass and may override any superclass methods.

我记得一个例子,其中泛化和继承之间的区别确实有意义:

正方形是长方形。这来自他们在数学中的定义:

  • 矩形是有四个直角的四边形
  • 正方形是具有四个相等边和角的多边形

在设计层面,正方形和长方形之间存在泛化关系。

但在实现层面,情况有所不同:

  • 一个矩形可以通过两个度量来定义:它的宽度和高度
  • 一个正方形可以用一个度量来定义,因为所有的边都是相等的

在 Square 类中继承 Rectangle 类的宽度和高度会适得其反,这将需要额外的约束以确保为宽度的 setter 和高度的 setter 提供相同的值。

在这种情况下(我承认有点急躁)您不会将泛化关系转换为在实现级别使用继承。

好吧,那是那些日子。

关于oop - UML 类图中的关系如何继承?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1988500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com