访问类属性和受保护的类



这是我家庭作业的一部分。我正在创建一个造船厂模拟器。造船厂由容器(链表)组成,这些容器内是包(也是链表的集合)。集装箱具有以下属性(目的地、重量限制、ID),包裹具有(所有者名称、目的地、重量、ID) 基本上,当我创建一个新包裹时,如果不存在具有相同目的地的容器,那么我需要在造船厂内创建一个新容器并将包裹放置在容器内;否则我只会将包裹放在相应的容器中。容器按字母顺序排序,包裹最轻的顺序在前。

这就是我目前所拥有的。我写了 add_container() 进行测试,它可以工作,但我不需要它;真正的工作是由 add() 方法完成的。它不起作用,我不知道为什么。如果你们中的任何人能给我一个理由甚至提示,我将不胜感激。

from random import *
#Package class
class Package:
def __int__(self, name, destination, weight):
self._name = name
self._destination = destination
self._weight = weight
self._Next = None
self._ID = 0
#Container Class
class Container:
def __init__(self, dest):
self._dest = dest
self._Front = None
self._next = None
self._Maxweight = 2000
self._size = 0
self._Identification = 0

class Shipyard:
def __init__(self):
self._Front = None
self._size = 0

def size(self):
return self._size

def add_container(self, Destination):
container = Container(Destination)
if self._Front == None:
self._Front = container
self._size += 1
container._Identification = ((randint(1,1999)))
elif container._dest < self._Front._dest:
container._next  = self._Front
self._Front = container
self._size += 1
container._Identification = ((randint(1, 1999)))
else:
current = self._Front
previous = None
while current is not None and current._dest < Destination:
previous = current
current = current._next
if current == None:
previous._next = container
self._size += 1
container._Identification = ((randint(1, 1999)))
else:
container._next = current
previous._next = container
self._size += 1
container._Identification = ((randint(1, 1999)))

def is_empty(self):
return self._size == 0

def  container_exists(self, dest):
container_found = False
First_container = self._Front
if First_container == None:
pass
else:
while container_found != True:
previous = First_container
First_container = First_container._next
if previous._dest == dest:
container_found = True
return True
else:
return False


def printAll(self):
current = self._Front
while current != None:
print(current._dest)
current = current._next

def add(self, name, destination, weight):
package = Package(name, destination, weight)
if self.container_exists(destination) == True:
current = self._Front
while current._dest != destination:
current = current._next

if current._dest == destination:
weightlimit = current._Maxtwiehgt - weight
if weightlimit <= 0:
print("Container to ", destination, "is full!")
return
if current._Front == None:
current._Front = package
current._Maxtweihgt -= weight
current._size += 1
current._Front._ID = (randint(1,1999))
else:
currentPackage = current._Front
previousPackage = None
while currentPackage._Next!= None:
previousPackage = currentPackage
currentPackage = currentPackage._Next
if currentPackage._weight > weight > previousPackage:
package._Next = currentPackage
previousPackage._Next = package
package._ID = (randint(1,1999))
current._Maxweight -= weight
current._size += 1

else:
container = Container(destination)
if self._Front == None:
self._Front = container
self._Front._Front = package #new package
self._Front._Identification = (randint(1,1999)) #container ID
self._Front._Maxweight -= weight #container weight
self._Front._Front._ID = (randint(1,1999)) #package id
self._size += 1  #shipyard size
self._Front._size += 1 #container size

elif self._Front._dest > destination:
container._next = self._Front
self._Front = container
self._Front._Front = package  # new package
self._Front._Identification = (randint(1, 1999))  # container ID
self._Front._Maxweight -= weight  # container weight
self._Front._Front._ID = (randint(1, 1999))  # package id
self._size += 1  # shipyard size
self._Front._size += 1  # container size

else:
current = self._Front
previous = None
while current._next != None:
previous = current
current = current._next
if current._dest > destination > previous._dest:
container._next = current
previous._next = container
container._Front = package
container._Identification = (randint(1,1999))
container._Maxweight -= weight
container._Front._ID = (randint(1,1999))
self._size += 1
container._size += 1


def main():
myYard = Shipyard()
myYard.add("Jamie", "Atlanta", 120)
main()

写下来:您看到的错误是:

TypeError: object() takes no parameters

当你看到object() takes no parameters时,它几乎总是意味着你搞砸了构造函数。

首先,检查下划线的数量。确保前面有两个下划线,后面有两个下划线。

接下来,确保您拼写正确__init__。例如,如果你要声明一个名为__int__的函数,它不会做你所希望的。:)

最新更新