如何從不同的類中創(chuàng)建兩個對象以相互引用?

我正在做一個項目,遇到了一個障礙。我不確定是否使不同類的對象相互引用,如果其中一個對象刪除了引用,則刪除該引用。

例如,我有一個網(wǎng)絡(luò)交換機(jī),交換機(jī)有端口。這些端口可能/可能沒有使用電纜連接到網(wǎng)絡(luò)上的其他交換機(jī)。請參考以下課程:

Port object:

class Port(object):
linkSpeeds = {'Fa' : 'Fast',
              'Gi' : 'Gigabit',
              'Te' : 'TenGigabit',
              'Fo' : 'FortyGigabit'}

def __init__(self, linkSpeed, brick, port, module=None, cable=None):
    self.__linkSpeed = self.linkSpeeds[linkSpeed]
    self.__brick = brick
    self.__port = port
    self.__modeule = module
    self.__cable = cable

def __repr__(self):
    outputString = '{} {}/{}/{}'
    return outputString.format(self.__linkSpeed[:2], self.__brick, self.__modeule, self.__port)

def connect(self, cableId):
    self.__cable = cableId

Cable Object:

from port import Port

class Cable(object):

cableNumber = 1

def __init__(self, connLeft = None, connRight = None):
    self.__connectionLeft = connLeft if isinstance(connLeft, Port) else None
    self.__connectionRight = connRight if isinstance(connRight, Port) else None
    self.__cableId = Cable.cableNumber
    Cable.cableNumber += 1

def __repr__(self):
    outputString = '{}  <----({})---->  {}'
    return outputString.format(self.__connectionLeft, self.__cableId, self.__connectionRight)

def setLeft(self, connLeft):
    self.__connectionLeft = connLeft if isinstance(connLeft, Port) else None

def setRight(self, connRight):
    self.__connectionRight = connRight if isinstance(connRight, Port) else None

如果我創(chuàng)建了一個電纜對象并設(shè)置了左端口和右端口,它將引用這些端口。但當(dāng)它出現(xiàn)時,我希望端口對象也開始引用該電纜對象,反之亦然。此外,如果我從電纜中刪除了leftConnection,我希望這些更改也會返回到端口。有沒有一種方法可以做到這一點(diǎn),而不必手動添加對兩個對象的引用?

? 最佳回答:

希望這個例子有幫助

class Port(object):
    linkSpeeds = {'Fa': 'Fast',
                  'Gi': 'Gigabit',
                  'Te': 'TenGigabit',
                  'Fo': 'FortyGigabit'}

    def __init__(self, linkSpeed, brick, port, module=None, cable=None):
        self.__linkSpeed = self.linkSpeeds[linkSpeed]
        self.__brick = brick
        self.__port = port
        self.__modeule = module
        self._cable = cable

    def __repr__(self):
        outputString = '{}{}/{}/{}'.format(self.__linkSpeed[:2], self.__brick, self.__modeule, self.__port)
        if self._cable:
            outputString = "{}-cable{}".format(outputString, self._cable)
        return outputString

    def connect(self, cableId):
        self._cable = cableId


class Cable(object):
    cableNumber = 1

    def __init__(self, connLeft=None, connRight=None):
        self.__connectionLeft = connLeft if isinstance(connLeft, Port) else None
        self.__connectionRight = connRight if isinstance(connRight, Port) else None
        self.__cableId = Cable.cableNumber
        Cable.cableNumber += 1

    def __repr__(self):
        outputString = '{}  <----({})---->  {}'
        return outputString.format(self.__connectionLeft, self.__cableId, self.__connectionRight)

    @property
    def left(self):
        return self.__connectionLeft

    @left.setter
    def left(self, port):
        """Join cable with port and port with cable"""
        self.__connectionLeft = None
        if isinstance(port, Port):
            self.__connectionLeft = port
            port._cable = self.__cableId

    @property
    def right(self):
        return self.__connectionRight

    @right.setter
    def right(self, port):
        """Join cable with port and port with cable"""
        self.__connectionRight = None
        if isinstance(port, Port):
            self.__connectionRight = port
            setattr(port, "_cable", self.__cableId)


# init ports and cable
port1 = Port(linkSpeed="Fa", brick="1", port="1", module="1")
port2 = Port(linkSpeed="Fa", brick="2", port="2", module="2")
cable = Cable()
print(port1)  # Fa1/1/1
print(cable)  # None  <----(1)---->  None
print(port2)  # Fa2/2/2
print()

# connect ports with cable
cable.left = port1
cable.right = port2
print(port1)  # Fa1/1/1-cable1
print(cable)  # Fa1/1/1-cable1  <----(1)---->  Fa2/2/2-cable1
print(port2)  # Fa2/2/2-cable1
主站蜘蛛池模板: 一区高清大胆人体| 国产产一区二区三区久久毛片国语| 亚洲中文字幕丝袜制服一区| 无码国产精品一区二区免费16| 久久青青草原一区二区| 精品一区二区三区四区| 国产在线精品一区二区夜色| 国产综合一区二区在线观看 | 一区二区三区国产精品| 熟女少妇丰满一区二区| 国产免费无码一区二区| 国产福利一区二区三区在线视频| 精品国产一区二区三区久久久狼| 亚洲福利一区二区精品秒拍| 极品人妻少妇一区二区三区| 欧洲精品免费一区二区三区 | 亚洲av无码一区二区三区人妖 | 无码人妻AⅤ一区二区三区| 高清精品一区二区三区一区| www.亚洲一区| 中文字幕日韩精品一区二区三区| 成人免费一区二区无码视频| 波多野结衣一区二区免费视频| 国产在线观看一区精品| 成人精品视频一区二区三区尤物| 久久精品无码一区二区app| 亚洲精品精华液一区二区 | 日韩人妻无码一区二区三区久久| 精品三级AV无码一区| 亚洲日本一区二区三区在线不卡| 日本高清一区二区三区| 国产高清视频一区三区| 视频一区精品自拍| 黄桃AV无码免费一区二区三区| 日韩最新视频一区二区三| 中文字幕一区二区三区5566| 久久久综合亚洲色一区二区三区 | 日本一区中文字幕日本一二三区视频| 色久综合网精品一区二区| 久久精品中文字幕一区| 一区二区三区在线观看视频|