![](/img/trans.png)
[英]How to represent the relationship between an instance of a class and the class that gets it as an input?
[英]How to show the relationship between a class and another class that takes the instance of the class's instance as an input with a sequence diagram?
我将使用上一个问题中的相同示例并对其进行修改。
我有一个名为House
的 class 。 这个 class 的实例是house
。
class House:
def __init__(self, steel, money):
self.steel = steel
self.money = money
def housePlan():
houseHeight = self.steel/self.money
houseEdgeLength = self.money
我还有另一个名为Person
的 class 。 这个 class 获取多个输入并创建一个House
实例。 没有Person
class, House
也可以存在。
class Person:
def __init__(self,name, steel, money):
self.name = name
self.steel = steel
self.money = money
def buildHouse():
house = House(self.steel, self.money)
如何用 UML 序列图显示这两个类之间的关系?
如何用 UML 序列图显示这两个类之间的关系?
序列图的目标不是显示类之间的关系,序列图通过关注交换的消息序列以及它们在生命线上的相应出现规范来描述交互(正式/2017-12-05 § 17.8 序列图表)
从您的代码buildHouse创建一个新的House实例,因此有一个object creation Message 。 因为house是一个局部变量,所以实例会立即丢失,然后我们可以认为它被 Python 的垃圾收集器立即删除,因此DestructionOccurrenceSpecification由生命线底部的 X 形式的叉表示(§ 17.4. 4.2 破坏发生规范)。
(我为buildHouse使用了一条found 消息,因为调用者未知且与您的问题无关)
房子可以在没有人 class 的情况下存在
如果您谈论 class,肯定是的,因为House定义没有嵌套在Person中。
如果您在全球范围内谈论实例,那么没有什么说只有Person可以实例化House ,所以也可以。
如果您在我的回答中提到您之前的问题,我不使用组合,因此删除Person的实例并不意味着删除关联的House实例。
但是再次在buildHouse中, House的新实例立即丢失,因为没有返回,也没有保存在全局变量中,也没有保存在Person的属性中,然后将被垃圾删除
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.