python - 我在从其他类继承时遇到问题

标签 python oop

我刚刚创建了三个继承类,但出了问题,但我尝试解决它但不起作用
我想知道是什么导致了这个错误以及我应该如何解决它
我应该从其他类继承

  1. 类(class):
    • 属性(property)
    • 房子
    • 出租
    • 房屋出租

为什么 House 对象不带参数

《Python 3 面向对象编程》一书中

这有点令人惊讶,因为它既没有 init 也没有显示方法!因为两个父类在这些方法中都适本地调用了 super,所以我们只需扩展这些类,并且这些类将以正确的顺序运行。当然,prompt_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

最佳答案

在 Rental 类中,您没有指定父类,但您调用了 super()。

租赁应该是属性(property)的子类吗?

如果是这样,只需将该类更改为:

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.__init__() takes no parameters

关于python - 我在从其他类继承时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54726778/

相关文章:

python - 无法访问返回的 h5py 对象实例

python - Docopt 在中间接受多个参数?

wcf - 如何实现所有方法都应该调用的基本方法?

oop - Chapel 中是否有与 Fortran 的 select 类型语句等效的语句?

java - 为什么 Checkstyle 默认不允许 protected 变量?

C++构造函数参数问题

python - "ValueError: Unsupported format character ' "' (0x22) 在...”在 Python/字符串

python - 使用字符串django自动递增id值

python - 圆点和十字 python 画板

c# - 如何正确命名域 namespace 内的类?