定义一个TextBook类,从Book类继承。请增加属性courseware和exercise,并重载info()方法,增加courseware和exercise信息输出。请实例化一个python_textbook对象,并调用info()方法,输出信息
时间: 2024-02-09 19:10:28 浏览: 127
定义一个Book类,实现简单操作实验报告
4星 · 用户满意度95%
好的,我已经了解了你的要求。下面是定义TextBook类的代码,包括从Book类继承、新增属性和重载info()方法:
```python
class Book:
def __init__(self, title, author, publisher, price):
self.title = title
self.author = author
self.publisher = publisher
self.price = price
def info(self):
print(f"《{self.title}》是由{self.author}撰写,由{self.publisher}出版,售价{self.price}元。")
class TextBook(Book):
def __init__(self, title, author, publisher, price, courseware, exercise):
super().__init__(title, author, publisher, price)
self.courseware = courseware
self.exercise = exercise
def info(self):
super().info()
print(f"附带课件{self.courseware}和习题{self.exercise}。")
```
接下来,我们可以实例化一个python_textbook对象,并调用info()方法输出信息:
```python
python_textbook = TextBook("Python编程入门", "张三", "人民邮电出版社", 49.9, "Python学习视频", "Python编程题库")
python_textbook.info()
```
输出结果为:
```
《Python编程入门》是由张三撰写,由人民邮电出版社出版,售价49.9元。
附带课件Python学习视频和习题Python编程题库。
```
阅读全文