我是初学者,我在这堂课和__init__
中遇到问题:
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
self.odometer_readings = 0
def getname(self):
longname = f'{self.year} {self.model} {self.year}'
return longname.title()
def read_odometer(self):
print(f'This car has {self.odometer_readings} miles on it')
def update_odometer(self, mileage):
if mileage >= self.odometer_readings:
self.odometer_readings = mileage
else:
print('You cant roll back the odometer')
def increment_odometer(self, mileage):
self.odometer_readings += miles
class Battery:
def __init__(self, battery_size = 75):
self.battery_size = battery_size
def describe_battery(self):
print(f'This car has a {self.battery_size}-kWh battery in it')
def get_range(self):
if self.battery_size == 75:
range = 260
elif self.battery_size == 1000:
range = 310
print(f"This car can go {range} miles on a full charge")
class electriccar:
def __init__(self, make, model, year):
super().__init__(make, model, year)
self.battery = Battery()
错误:
Traceback (most recent call last):
File "C:/Users/Teerth Jain/AppData/Local/Programs/Python/Python37-32/mycar.py", line 2, in <module>
mycar = electriccar('tesla', 'ashduf', 297)
File "C:/Users/Teerth Jain/AppData/Local/Programs/Python/Python37-32\car.py", line 46, in __init__
super().__init__(make, model, year)
TypeError: object.__init__() takes exactly one argument (the instance to initialize)
答案 0 :(得分:0)
声明电动汽车的基类:
class electriccar(Car):
....