我刚刚创建了三个用于继承的类,但是出了点问题,但是我尝试解决了这个问题,但是没用
我想知道导致此错误的原因以及应该如何解决它
我应该继承其他课程
为什么房屋对象不带参数
在图书python 3面向对象编程中
这有点令人惊讶,因为它既没有 init 也没有显示方法!因为两个父类都在这些方法中适当地调用了super,所以我们只需要扩展这些类,并且这些类将以正确的顺序运行。当然,对于alert_init来说不是这种情况,因为它是一个静态方法,不会调用super,所以我们明确实现了这一方法。在编写其他三个组合之前,我们应该测试该类以确保其行为正确:
def get_valid_input(input_string, valid_options):
input_string += "({})".format(", ".join(valid_options))
response = input(input_string)
while response.lower() not in valid_options:
response = input(input_string)
return response
class Property:
def __init__(self, baths="", square_feet="",
beds="", **kwargs):
super().__init__(**kwargs)
self.num_baths = baths
self.square_feet = square_feet
self.num_beds = beds
def display(self):
print("PROPERTY DETAILS")
print("================")
print("square footage: {}".format(self.square_feet))
print("bedrooms: {}".format(self.num_bedrooms))
print("bathrooms: {}".format(self.num_baths))
print()
@staticmethod
def prompt_init():
return dict(sqare_feet=input("Enter The Square:"),
num_beds=input("Enter the Number of beds"),
num_baths=input("Enter the Number of baths"),)
class House(Property):
valid_garage = ("attached", "detached", "none")
valid_fenced = ("yes", "no")
def __init__(self, garage="", fenced="", num_stories="", **kwargs):
super().__init__(**kwargs)
self.num_stories = num_stories
self.garage = garage
self.fenced = fenced
def display(self):
super().display()
print("HOUSE DETAILS")
print("# of stories: {}".format(self.num_stories))
print("garage: {}".format(self.garage))
print("fenced yard: {}".format(self.fenced))
@staticmethod
def prompt_init():
parent_init = Property.prompt_init()
garage = get_valid_input("Is the yard fenced? ", House.valid_garage)
fenced = get_valid_input("Is there a garage? ", House.valid_fenced)
num_stories = input("How many stories? ")
parent_init.update({
"garage": garage,
"fenced": fenced,
"num_stories": num_stories
})
return parent_init
class Rental:
def __init__(self, furnished="", rent="", utilities="", **kwargs):
super().__init__(**kwargs)
self.furnished = furnished
self.rent = rent
self.utilities = utilities
def display(self):
super().display()
print("RENTAL DETAILS")
print("rent: {}".format(self.rent))
print("estimated utilities: {}".format(self.utilities))
print("furnished: {}".format(self.furnished))
@staticmethod
def prompt_init():
return dict(
rent=input("What is the monthly rent? "),
utilities=input("What are the estimated utilities? "),
furnished=get_valid_input("Is the property furnished? ", ("yes", "no")),)
class HouseRental(House, Rental):
@staticmethod
def prompt_init():
init = House.prompt_init()
init.update(Rental.prompt_init())
return init
info = HouseRental().prompt_init()
o = HouseRental(**info)
o.display()
Traceback (most recent call last):
File "estate/placement.py", line 148, in <module>
o = HouseRental(**info)
File "estate/placement.py", line 68, in __init__
super().__init__(**kwargs)
File "estate/placement.py", line 13, in __init__
super().__init__(**kwargs)
File "estate/placement.py", line 117, in __init__
super().__init__(**kwargs)
TypeError: object.__init__() takes no parameters
答案 0 :(得分:2)
在Rental类中,您没有指定父类,但是您已经调用了super()。
出租应该是财产的子类吗?
如果是这样,只需将该类更改为:
class Rental(Property):
def __init__(self, furnished="", rent="", utilities="", **kwargs):
super().__init__(**kwargs)
self.furnished = furnished
self.rent = rent
self.utilities = utilities
类似地,类Property调用super(),但没有父类继承。我不认为您打算让Property成为子类,因此请删除super()调用:
class Property:
def __init__(self, baths="", square_feet="",
beds="", **kwargs):
self.num_baths = baths
self.square_feet = square_feet
self.num_beds = beds
更一般而言: 形式NewClass(ParentClass)诱导NewClass从ParentClass继承方法和属性。现在,可以将Parent类的 init 函数采用的所有参数安全地传递给NewClass。 调用super()。 init (** kwargs)会将传递给NewClass的所有关键字参数传递给ParentClass。
如果没有ParentClass,则NewClass继承自Python基类Object,该基类不带任何参数。将(** kwargs)传递给Object会引发错误。
回溯的最后一行对此进行了描述:
object.__init__() takes no parameters