请用Book实例化一个python_book对象,并调用info()方法,输出信息
时间: 2024-02-09 13:09:25 浏览: 190
好的,我可以使用如下代码实例化一个python_book对象,并调用它的info()方法输出信息:
```python
class Book:
def __init__(self, title, author, publisher, year):
self.title = title
self.author = author
self.publisher = publisher
self.year = year
def info(self):
print(f"Title: {self.title}")
print(f"Author: {self.author}")
print(f"Publisher: {self.publisher}")
print(f"Year: {self.year}")
python_book = Book("Learning Python", "Mark Lutz", "O'Reilly Media", 2013)
python_book.info()
```
输出的信息为:
```
Title: Learning Python
Author: Mark Lutz
Publisher: O'Reilly Media
Year: 2013
```
相关问题
定义一个TextBook类,从Book类继承。请增加属性courseware和exercise,并重载info()方法,增加courseware和exercise信息输出。请实例化一个python_textbook对象,并调用info()方法,输出信息
好的,我已经了解了你的要求。下面是定义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编程题库。
```
py请定义一个TextBook类,从Book类继承,教材相比普通书籍需要配套课件和练习题, 所以请增加属性:courseware和exercise, 并重载info方法,增加courseware和exercise信息输出。请实例化一个python_textbook对象, 并调用info()方法,输出信息.
好的,以下是定义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编程基础练习题
```
阅读全文