在这里我写了Book的类,让TextBook去继承它,但在继承时,super()报错,我逻辑写的没错啊,但为什么保错了呢
#上述代码中的两个问题:
# ① 在定义子类的构造函数时,父类构造函数中的参数必须添加上,而且要在子类参数之前
# ② format格式化字符串序号是从 0 开始的
class Book(object):
def __init__(self,name,author,isbn,publisher,price):
self.name = name
self.author = author
self.isbn = isbn
self.publisher = publisher
self.price = price
def info(self):
print('实例化调用{}/{}/{}/{}/{}'.format(self.name,self.author,self.isbn,self.publisher,self.price) )
class TextBook(Book):
# 在定义子类的构造函数时,父类构造函数中的参数必须添加上,而且要在子类参数之前
def __init__(self,name,author,isbn,publisher,price,courseware,exercise):
super().__init__(name,author,isbn,publisher,price)
self.courseware = courseware
self.exercise = exercise
def info(self):
# format格式化字符串序号是从 0 开始的
print('重写info()函数后的结果:{0}-{1}-{2}-{3}-{4}-{5}-{6}'.format(self.name,self.author,self.isbn,self.publisher,self.price,self.courseware,self.exercise))
python_textbook = TextBook('name','author','isbn','publisher','price','courseware','exercise')
python_textbook.info()
报错是变量没有定义