简体   繁体   English

如何使用序列图显示 class 和另一个 class 之间的关系,该 class 将类实例的实例作为输入?

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

I will use the same example from my previous question and modify it.我将使用上一个问题中的相同示例并对其进行修改。

I have a class called House .我有一个名为House的 class 。 The instance of this class is house .这个 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

I have another class called Person .我还有另一个名为Person的 class 。 This class gets several inputs and creates an instance of House .这个 class 获取多个输入并创建一个House实例。 House can exist without the Person class.没有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)

How can I show the relationship between these two classes with UML sequence diagrams?如何用 UML 序列图显示这两个类之间的关系?

How can I show the relationship between these two classes with UML sequence diagrams?如何用 UML 序列图显示这两个类之间的关系?

The goal of a Sequence Diagram is not to show relationships between classes, a Sequence Diagram describes an Interaction by focusing on the sequence of Messages that are exchanged, along with their corresponding OccurrenceSpecifications on the Lifelines ( formal/2017-12-05 § 17.8 Sequence Diagrams )序列图的目标不是显示类之间的关系,序列图通过关注交换的消息序列以及它们在生命线上的相应出现规范来描述交互正式/2017-12-05 § 17.8 序列图表

From your code buildHouse creates a new instance of House so there is an object creation Message .从您的代码buildHouse创建一个新的House实例,因此有一个object creation Message Because house is a local variable the instance is immediately lost and then we can consider it is immediately deleted by the garbage collector of Python, so a DestructionOccurrenceSpecification depicted by a cross in the form of an X at the bottom of a Lifeline (§ 17.4.4.2 DestructionOccurrenceSpecification ).因为house是一个局部变量,所以实例会立即丢失,然后我们可以认为它被 Python 的垃圾收集器立即删除,因此DestructionOccurrenceSpecification由生命线底部的 X 形式的叉表示(§ 17.4. 4.2 破坏发生规范)。

在此处输入图像描述

(I used a found message for buildHouse because the caller is unknown nor relevant in your question) (我为buildHouse使用了一条found 消息,因为调用者未知且与您的问题无关)

House can exist without the Person class房子可以在没有人 class 的情况下存在

If you speak about the class, yes for sure because House definition is not nested in Person .如果您谈论 class,肯定是的,因为House定义没有嵌套在Person中。

If you globally speak about instances, there is nothing saying only a Person can instantiate a House so yes too.如果您在全球范围内谈论实例,那么没有什么说只有Person可以实例化House ,所以也可以。

If you refer to your previous question in my answer I do not use a composition so the deletion of an instance of Person does not imply the deletion of the associated instance of House .如果您在我的回答中提到您之前的问题,我不使用组合,因此删除Person的实例并不意味着删除关联的House实例。

But again in buildHouse the new instance of House is immediately lost because not returned nor saved in a global variable nor saved in an attribute of Person , and then will be deleted by the garbage但是再次在buildHouse中, House的新实例立即丢失,因为没有返回,也没有保存在全局变量中,也没有保存在Person的属性中,然后将被垃圾删除

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何表示 class 的实例与将其作为输入的 class 之间的关系? - How to represent the relationship between an instance of a class and the class that gets it as an input? 用另一个 class 实例更新 class 实例的 __dict__ - Update a class instance's __dict__ by another class instance 如何使用另一个类的实例作为属性实例化一个类? - How to instantiate a class with another class' instance as attribute? 如何在Python中从一个类访问类方法和类变量到另一个类的实例方法? - How to access class method and class variable from a class to another class's instance method in Python? 如何在另一个类的实例中存储类实例的变量 - How to store a variable of an instance of a class in an instance of another class 如何键入一个 function,它采用 class 或 class 的一个实例,并返回该 class 的一个实例? - How to type a function that takes either a class, or an instance of the class, and returns an instance of that class? 使用 Joblib 将类对象实例作为输入参数的并行化函数 - Parallelizing function which takes a class object instance as input argument with Joblib 将类的实例(类的对象)传递给另一个类 - Passing an instance of a class (object of a class) to another class 如何从另一个人的类方法调用或引用实例属性 - How to call or refer to an instance attribute from another's class method 如何将一个类的实例方法monkeypatch到另一个类? - How to monkeypatch one class's instance method to another one?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM