【问题标题】:Inherited Classes; How to implement继承类;如何实施
【发布时间】:2021-12-15 00:55:28
【问题描述】:

我在类中继承和插入数据有错吗?

class Car:
    def __init__(self, make, model, year):
        self.make = "Honda"
        self.model = "Minivan"
        self.year = "2010"

class Entry(Car):
    def __init__(self, make, model, year, driver, number):
        super().__init__(make, model, year)
        self.driver = "Mike Drosser"
        self.number = 9
    def printEntry(self):
        print("The make is: "+self.make)
        print("The model is: "+self.model)
        print("The year is: "+self.year)

        print("The driver is: "+self.driver)
        self.number = str(self.number)
        print("The number is: "+self.number)

x = Entry()
x.printEntry()

我必须单独提交Car类数据吗?

【问题讨论】:

  • 您定义它的方式、品牌、型号、年份、驱动程序、编号都是必填参数
  • 您应该提及未按预期工作的内容(错误消息等),但乍一看,您正在创建一个 Entry 对象,而没有将任何必需的参数传递给构造函数。跨度>
  • 我认为您的意思是将这些值作为默认参数。
  • 请阅读How to Askmeta.stackoverflow.com/questions/359146。如果你想问“我做错了什么”,那么你应该主动解释为什么你认为有问题——最好是通过展示证据。运行代码时会发生什么?应该发生什么,有什么不同?
  • 无论如何,我不认为这真的是关于课程的问题。如果不是在两个单独的类中使用两个单独的 __init__ 方法,而是编写了具有相同功能的名为 make_carmake_entry 的普通函数(没有类),我想您会立即看到问题。此外,如果您想弄清楚如何让类与玩具示例一起工作,并遇到这样的问题,请考虑阅读并遵循教程,而不是尝试询问 Stack Overflow。或者至少考虑使用实际讨论论坛,例如 Reddit 或 Quora。

标签: python class object


【解决方案1】:

您的代码有 2 个问题:

  1. 构造函数中的参数都是必需的,因此在执行Entry() 时会失败,因为没有提供任何参数。

  2. 您不会对构造函数提供的值做任何事情,而是将特定值存储在您的实例属性中。

除此之外,其他一切看起来都很好。

一个简单的解决方法是

class Car:

    def __init__(self, make, model, year):
        self.make = make #we store the provided value
        self.model = model
        self.year = year

class Entry(Car):

    def __init__(self, make, model, year, driver, number):
        super().__init__(make, model, year)
        self.driver = driver
        self.number = number
        
    def printEntry(self):
        print("The make is: "+self.make)
        print("The model is: "+self.model)
        print("The year is: "+self.year)

        print("The driver is: "+self.driver)
        self.number = str(self.number)
        print("The number is: "+self.number)

x = Entry("Honda", "Minivan", "2010", "Mike Drosser", 9) #this is how a proper call of this class would look like
x.printEntry()

还有其他可以改变的东西,比如你的打印,打印函数可以接受任意数量的参数,所以这个print("The number is: "+self.number)可以更改为print("The number is:", self.number)这样你就不需要显式转换它如果是数字或任何非字符串,则首先转换为字符串。

但是为了更加pythonic,把printEntry方法改成__str__这个特殊方法,这样就可以在里面调用print了

class Car:

    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year
        
    def __str__(self):
        #this method should return a string representation of this class, 
        #for this we use f-string to format the different attributes
        return f"The make is: {self.make}\nThe model is: {self.model}\nThe year is: {self.year}"

class Entry(Car):

    def __init__(self, make, model, year, driver, number):
        super().__init__(make, model, year)
        self.driver = driver
        self.number = number
        
    def __str__(self):
        #here we make use of inheritances to split job 
        #by letting the Car class handle the part it 
        #know about and this subclass will add to it 
        #with the part exclusive to it
        return super().__str__() + f"\nThe driver is: {self.driver}\nThe number is: {self.number}"
        
        

x = Entry("Honda", "Minivan", "2010", "Mike Drosser", 9)
print(x)

【讨论】:

  • 我得到这个错误`文件“part4.py”,第 27 行,在 x = Entry("Honda", "Minivan", "2010", "Mike Drosser", 9) #this 是这个类的正确调用看起来像 File "part4.py", line 14, in init super().__init__(make, model, year) TypeError: super() takes至少 1 个参数(给定 0 个)`
  • @ArcOfficial 这个示例代码没有给出错误,super 都不需要任何参数,你的那个文件中还有其他事情导致了这个错误
  • 实际上第二段代码返回这个:File "part4.py", line 14 return f"The make is: {self.make}\nThe model is: {self.model}\nThe year is: {self.year}" ^ SyntaxError: invalid syntax
  • 语法错误?你用的是什么版本的python?
  • 很酷,你注意到 python 版本之间有很多小的差异,f-string 是在 3.6 中引入的,而 super 是从 2 中的需要参数更改为 3 中不需要它们
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多