[英]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.