【发布时间】:2021-06-02 05:16:35
【问题描述】:
我刚开始学习 UML,目前正在尝试使用 python 实现此图表的功能。
我已经写了继承部分,但我还不知道如何处理它的关联部分。谁能给我一些关于如何做到这一点的指示?尤其是循环引用。
到目前为止我的实现:
class RailCar():
def __init__(self, name):
if len(name) >= 2:
self.__name = name
self.locomotive = None
else:
raise Exception("Name length of", type(self), " object must be at least 2 characters long.")
class PassengerCar(RailCar):
def __init__(self, capacity):
super().__init__(self, name)
self.__capacity = capacity
class RestaurantCar(RailCar):
def __init__(self, first):
super().__init__(self, name)
self.__first = first
class Locomotive():
def __init__(self, railCar):
self.railCar = railCar
rc01 = RailCar('01')
rc01.locomotive = "It works!!"
print(rc01.locomotive)
【问题讨论】:
-
不幸的是,自关联没有命名/没有任何作用。所以很难说它可能是什么。大概然后是拖车?在这种情况下,只需在
RailCar内添加一个self.nextRailsCar或类似名称。
标签: python inheritance uml associations