这是个大问题,我深表歉意。
我正在尝试使编码更具模块化。 我们的任务是创建一个造船厂系统。 造船厂有集装箱,集装箱有包裹。
提供了一个可导入的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)?
答案 0 :(得分:1)
您可以在新类中继承SortedLList并重载所需的方法。
class MySortedLList(SortedLList):
def traversePrint(self):
# Your custom implementation
,然后使用它代替SortedLLIst,例如:
class Shipyard:
def __init__(self):
self._container = MySortedLList()
但是,您询问是否可以使用SortedLList.traversePrint
打印有关您的主程序(可能是Shipyard
)的信息。这可能没有意义,因为您将破坏封装。 SortedLList
知道其元素,并且只有在引用Shipyard
的情况下才能知道Shipyard
。因此,让Shipyard
向您介绍一下自己。