py请定义一个TextBook类,从Book类继承,教材相比普通书籍需要配套课件和练习题, 所以请增加属性:courseware和exercise, 并重载info方法,增加courseware和exercise信息输出。请实例化一个python_textbook对象, 并调用info()方法,输出信息.
时间: 2023-12-03 12:46:04 浏览: 176
定义一个Book类,实现简单操作实验报告
4星 · 用户满意度95%
好的,以下是定义TextBook类的代码:
```python
class Book:
def __init__(self, title, author, price):
self.title = title
self.author = author
self.price = price
def info(self):
print(f"书名:{self.title}\n作者:{self.author}\n价格:{self.price}元")
class TextBook(Book):
def __init__(self, title, author, price, courseware, exercise):
super().__init__(title, author, price)
self.courseware = courseware
self.exercise = exercise
def info(self):
super().info()
print(f"课件:{self.courseware}\n练习题:{self.exercise}")
python_textbook = TextBook("Python编程基础", "John Smith", 89.9, "Python编程基础课件", "Python编程基础练习题")
python_textbook.info()
```
输出结果为:
```
书名:Python编程基础
作者:John Smith
价格:89.9元
课件:Python编程基础课件
练习题:Python编程基础练习题
```
阅读全文