0
我正在通過製作汽車程序來處理Python中的繼承,但遇到了構建問題。下面是我的代碼:在Python汽車計劃中的繼承
class Car():
"""A simple attempt to represent a car"""
def __init__(self, make, model, year):
"""Initialize attributes to describe a car."""
self.make = make
self.model = model
self.year = year
#setting a default value for an attribute#
self.odometer_reading = 0
def get_descriptive_name(self):
"""Return a neatly formatted descriptive name."""
long_name = str(self.year) + ' ' + self.make + ' ' + self.model
return long_name.title()
def read_odometer(self):
"""Print a statement showing the car's mileage."""
print("This car has " +str(self.odometer_reading) + " miles on it.")
def update_odometer(self, mileage):
"""Modifying the value through the following method
Reject the change if it attempts to roll the odometer back
"""
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You can't roll back an odometer!")
def increment_odometer(self, miles):
"""Incremeting an attributes value through methods"""
self.odometer_reading += miles
class ElectricCar(Car):
"""Represent aspects of a car, specific to electric vehicles."""
def __init__(self, make, model, year):
"""Initialize attributes of the parent class."""
super().__init__(make, model, year)
my_tesla = ElectricCar('tesla', 'model s', 2016)
print(my_tesla.get_descriptive_name())
但是,我越來越想運行的程序時,此錯誤消息:
Traceback (most recent call last):
File "electric_car.py", line 39, in <module>
my_tesla = ElectricCar('tesla', 'model s', 2016)
File "electric_car.py", line 37, in __init__
super().__init__(make, model, year)
TypeError: super() takes at least 1 argument (0 given)
任何想法?
錯誤似乎很清楚:*「TypeError:super()至少需要1個參數(給出0)」*您可能正在使用Python 2.x. –
啊,就是這樣,似乎我需要升級到Python 3.謝謝! –
另外,你可能想從'object'擴展'Car' –