访问类属性和受保护的类

时间:2017-03-24 04:36:25

标签: python python-3.x

这是我作业的一部分。我正在创建一个造船厂模拟器。 Shipyard由容器(链接列表)组成,这些容器内部是包(也是链表的集合)。容器具有以下属性(Destination,weightlimit,ID)和parcels(所有者名称,目标,权重,ID)基本上,当我创建一个新Package时,如果不存在具有相同目标的容器,那么我需要在造船厂内创建一个新容器并将包装放在容器内;否则我只是将包放在相应的容器中。容器按字母顺序排列,包装按照最轻的顺序排列。

这是我到目前为止所拥有的。我编写了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()

1 个答案:

答案 0 :(得分:0)

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

TypeError: object() takes no parameters

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

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

接下来,请确保您已正确拼写__init__。例如,如果您要声明一个名为__int__的函数,它就不会按照您的意愿执行。 :)