定义一个book类 属性 : name ,author , isbn , publishes 和 , price , 方法 : info(), 输出 name , author , isbn , publishes 和 price。
时间: 2023-11-05 08:04:13 浏览: 96
以下是一个 Python 3 中的 book 类定义:
```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("Name:", self.name)
print("Author:", self.author)
print("ISBN:", self.isbn)
print("Publisher:", self.publisher)
print("Price:", self.price)
```
这个类有五个属性,分别是书名(name)、作者(author)、ISBN 号(isbn)、出版社(publisher)和价格(price),以及一个 info() 方法,用于打印出这些属性的值。在创建一个 book 对象时,需要传入这些属性的值,如下所示:
```python
my_book = Book("The Catcher in the Rye", "J.D. Salinger", "0316769177", "Little, Brown and Company", 8.99)
my_book.info()
```
输出:
```
Name: The Catcher in the Rye
Author: J.D. Salinger
ISBN: 0316769177
Publisher: Little, Brown and Company
Price: 8.99
```
阅读全文