Python中的继承错误

时间:2015-12-02 15:28:27

标签: python class python-3.x inheritance constructor

我正在尝试创建一个有房地产经纪人的程序。在这个计划中,我试图涵盖所有基本参数的房屋出租方面。但是我有一些错误。

class Apartment:


    def __init__(self, aptNumber, address, bedrooms, baths):
        self._aptNumber = aptNumber
        self._address = address
        self._bedrooms = int(bedrooms)
        self._baths = float(baths)

    def _securiyDep(self):
        securityDeposit =   330(bedrooms)

    def _rent(self):
        rent = 250(bedrooms) + 150(bathrooms)+200

    def _renter(self):
        renter = "vacant"

    def setSecDep(self, deposit):
        self._deposit = 1000

    def setRent(self, rentMoney):
        self._rentMoney = 800

    def setRenter(self, renter):
        self._renter = "Vacant"

    def getData(self, Data):
        self._Data = Data

    def isVacant(self, vacancy):
        self._vacancy = "True" or "False"


        def __repr__(self):
            s = self._aptNumber+ " located at " + str(self._address) + " is rented at" + self._rent
            s+= " and has a security deposit = " + self._deposit + " and is = " + self._vacancy
            return s





# Test code for the Apartment Class
if __name__ == "__main__":
    apts = []
    apts.append(Apartment("10C", "107 E. Main", 3, 1.5))
    apts.append(Apartment("14B", "109 E. Main", 4, 2))
    apts.append(Apartment("13", "2207 W. Broadway", "5", "2.5"))

    for apt in apts:
        print(apt)
    print()

    apts[0].setRent("1245")
    apts[0].setRenter("Rocky Quartzite")
    apts[1].setSecDep("1000")
    apts[1].setRenter("Millie Milton")

    print(apts[0].getData())
    print()

    for apt in apts:
        if not apt.isVacant():
            print(apt)
    print()

    apts[0].setRenter("")
    print(apts[0])

我有错误

    print(apts[0].getData())
<__main__.Apartment object at 0x0000000003124208>
TypeError: getData() missing 1 required positional argument: 'Data'
<__main__.Apartment object at 0x0000000003124240>

有人可以帮助解决错误吗?

2 个答案:

答案 0 :(得分:0)

你在第60行没有参数的情况下致电getDataprint(apts[0].getData())。在您的定义中,它需要一个名为Data的参数。

答案 1 :(得分:0)

您定义getData采用两个参数,即self,它是对您正在操作的实例的引用,并且是&#34;自动&#34;提供,第二个参数是Data

def getData(self, Data):
    self._Data = Data

但是当你拨打getData时,你不会提供第二个参数Data

apts[0].getData()

因此,解释器会告诉您缺少1个必需参数,在您的情况下为Data

由于您案例中的getData实际上设置了Data变量,因此最好将其称为setData

如果你想要检索Data变量,那么getter就必须这样做

def getData(self):
    return self._Data