重载,引用以及介于两者之间的所有内容

 收藏

这很重要,我深表歉意。

我正在练习使我的编码更具模块化。 我们的任务是创建一个造船厂系统。 造船厂有集装箱,集装箱中有包裹。

提供了一个可导入的LinkedList程序。我在底部添加了它。

重要说明:我无法以任何方式更改排序的链接列表程序,原因是使用超载程序

我有另一个非模块化程序。它是专门为手头的任务而设计的,并且在大多数情况下都有效。

Python 3.5

from SortedLList import *

class Shipyard:
    def __init__(self):
        self._container = SortedLList()

    def add(self, owner, destination, weight):
        """
        This function does:
        1. Adds a container for a specific destination if doesn't exist.
        2. Adds a package in a container given that the total weight 
        doesn't exceed 2000 lbs
        3. If it does, it creates another container headed in the same
        direction and then the package is inserted there instead.
        """
        self._container.insert(Self.Container(destination))

    class Container:
        def __init__(self, destination):
            self._package = SortedLList()
            self._dest = destination
            self._weight = 0
            self._max = 2000

       def add_pack(self, owner, destination, weight):
           """
           This function adds the weight of the package to the total
           container weight. And inserts a Package Singly Linked List
           object inside the container.
           """
           self._weight += weight
           self._package.insert(Self.Package(destination))

       def __lt__(self, other):
           return self._dest < other._dest

       def __ge__(self, other):
           return self._dest >= other._dest

       def __eq__(self, other):
           return self._dest == other._dest

       class Package:
           def __init__(self, owner, destination, weight):
               self._owner = owner
               self._dest = destination
               self._weight = weight

           def __lt__(self, other):
               return self._weight < other._weight

           def __ge__(self, other):
               return self._weight >= other._weight

           def __eq__(self, other):
              return self._weight == other._weight


class SortedLList :
    class _Node :
        def __init__(self, elem, next) :
            self._elem = elem
            self._next = next

    def __init__(self) :
        self._first = None
        self._size = 0

    def __len__(self) :
        return self._size

    def isEmpty(self) :
        return len(self) == 0

    def first(self):
        return self._elem._first

    def insert(self, val) :  
        if (self.isEmpty() or val <self._first._elem):
            self._size+=1
        self._first = self._Node(val,self._first)

        tmpRef=self._first

        while(tmpRef._next!=None and val>=tmpRef._next._elem):
            tmpRef=tmpRef._next

        if val==tmpRef._elem:
            return
        self._size+=1
        tmpRef._next=self._Node(val,tmpRef._next)
        return

    def isPresent(self, elem) :
        tmpRef=self._first
        while(tmpRef!=None):
            if tmpRef._elem==elem:
                return True
            tmpRef=tmpRef._next
        return False

    def delete(self, elem) :
        if self.isEmpty() :
            return
        if elem == self._first._elem :
            self._size -= 1
            self._first = self._first._next
            return

        tmpRef = self._first
        while (tmpRef._next != None and elem > tmpRef._next._elem) :
            tmpRef = tmpRef._next

        if tmpRef._next == None : return

        if tmpRef._next._elem != elem : return

        self._size -= 1
        tmpRef._next = tmpRef._next._next
        return


    def traversePrint(self) :
        tmpRef = self._first
        while tmpRef != None :
            print(tmpRef._elem)
            tmpRef = tmpRef._next

    class Empty(Exception) :
        pass

我希望能够使用SortedLList程序中的方法来显示我在主程序中拥有的信息。

有没有办法解决输入: 通过使用traversePrint()来打印(self._cont._first._elem._dest)或print(self._cont._first._elem._package._first._elem._owner)而不更改帮助程序代码?

回复
  • eipsum 回复

    您可以在新类中继承已排序列表,并覆盖所需的方法。

    class MySortedLList(SortedLList):
        def traversePrint(self):
            # Your custom implementation
    

    然后使用它而不是SortedLLIst,例如:

    class Shipyard:
        def __init__(self):
            self._container = MySortedLList()
    

    However,, you asked if you can use the SortedLList.traversePrint to print information about your main program, presumable Shipyard. This probably doesn't make sense because you'd break encapsulation. SortedLList knows about its elements and can only know about Shipyard if you give it a reference to Shipyard. So rather let Shipyard tell you about itself.