|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
1、下面子类添加属性时提示错误
class Car():
"""一次模拟汽车的简单尝试"""
def __init__(self,make,model,year):
"""初始化描述性汽车属性"""
self.make= make
self.model = model
self.year = year
self.odometer_reading=0
def get_descriptive_name(self):
"""返回整洁的描述性信息"""
long_name=str(self.year) + ' ' + self.make + ' ' + self.model
return long_name.title()
def read_odometer(self):
"""打印一条之处汽车里程的消息"""
print ("\nThis car has " + str(self.odometer_reading)+" miles on it.")
def update_odometer(self,mileage):
"""将里程表读数设置为指定的值"""
if mileage>=self.odometer_reading:
self.odometer_reading=mileage
else:
print("You can't roll back an odometer!")
def increment_odometer(self,miles):
"""将里程表读数增加指定的量"""
self.odometer_reading += miles
class ElectricCar(Car):
"""电动汽车的独特之处,初始化父类属性,再初始化电动汽车特有的属性"""
def __init_(self,make,model,year,bbb):
super().__init__(make,model,year)
self.bbb = 0
def describe_battery(self):
"""打印一条描述电瓶容量的消息"""
print(self.bbb)
my_tesla= ElectricCar('audi','a4',2016)
my_tesla.describe_battery()
错误提示:
Traceback (most recent call last):
File "C:\Users\WJW\Desktop\Python\Car.py", line 47, in <module>
my_tesla= ElectricCar('audi','a4',2016,99)
TypeError: __init__() takes 4 positional arguments but 5 were given
2、下面的程序和这个逻辑应该是一样的,但是运行正确
class AA():
def __init__(self,a,b):
self.a=0
self.b=0
print (str(b)+str(a))
class BB(AA):
def __init__(self,a,b,c):
super().__init__(a,b)
self.c=0
print( c + a + b )
a=AA(80,90)
b=BB(22,33,55)
运行结果
9080
3322
110
求大神解答,到底哪里出了问题 |
|