定义一个Book类,属性:name、author、isbn、publisher和price,方法:info(),输出name、author、isbn、publisher、price
时间: 2023-07-04 08:10:57 浏览: 357
好的,以下是定义一个Book类的Python代码,满足您的要求:
```python
class Book:
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(f"Name: {self.name}\nAuthor: {self.author}\nISBN: {self.isbn}\nPublisher: {self.publisher}\nPrice: {self.price}")
```
其中,`__init__`方法用于初始化类的属性,`info`方法用于输出书籍信息。可以使用以下代码创建一个Book对象并输出信息:
```python
book = Book("Python for Data Science Handbook", "Jake VanderPlas", "9781491912058", "O'Reilly Media", 39.99)
book.info()
```
输出结果为:
```
Name: Python for Data Science Handbook
Author: Jake VanderPlas
ISBN: 9781491912058
Publisher: O'Reilly Media
Price: 39.99
```
阅读全文