簡體   English   中英

為什么我得到“NoneType” object 沒有屬性“嗶”?

[英]Why am I getting 'NoneType' object has no attribute 'beep'?

class Vehicle():
    def __init__(self,price,color,gas):
        self.price = price
        self.gas = gas
        self.color = color
    
    def hello(self):
        print('hi how are you?')

    def fillUpTank(self):
        self.gas = 100

    def emptyTank(self):
        self.gas = 0

    def gasLeft(self):
        return self.gas


def Car(Vehicle):
    def __init__(self,price,color,gas,speed):
        super.__init__(self,price,color,gas)
        self.speed = speed 

    def beep(self):
        print('Beep Beep!')

    def hello(self):
        print('beep!')


jim = Vehicle(200,'blue','petrol'`
jim.hello()
tim = Car(500)
tim.beep() 

當我嘗試運行 jim.hello() 時,它沒有顯示任何錯誤,但是當我嘗試運行 tim.beep() 時

請在Car class not def之前使用class關鍵字。 實際上,您將Car定義為 function,請使用class關鍵字將其定義為 class。

class Car(Vehicle):
    def __init__(self,price,color,gas,speed):
        super.__init__(self,price,color,gas)
        self.speed = speed 

    def beep(self):
        print('Beep Beep!')

    def hello(self):
        print('beep!')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM