设计book类,要求:1)book类的成员属性包括:书名name(string类型)、出版日期publish
时间: 2024-02-23 17:58:31 浏览: 136
用c++定义一个Book(图书)类
5星 · 资源好评率100%
(date类型)、作者author(string类型)、价格price(float类型)、出版社publisher(string类型)。2)book类要有构造函数,能够初始化成员属性。3)book类要有get/set方法,能够对成员属性进行读取和设置。4)book类要有一个display方法,能够打印出书籍的所有信息。以下是一个可能的实现:
```python
class Book:
def __init__(self, name, publish_date, author, price, publisher):
self.name = name
self.publish_date = publish_date
self.author = author
self.price = price
self.publisher = publisher
def get_name(self):
return self.name
def set_name(self, name):
self.name = name
def get_publish_date(self):
return self.publish_date
def set_publish_date(self, publish_date):
self.publish_date = publish_date
def get_author(self):
return self.author
def set_author(self, author):
self.author = author
def get_price(self):
return self.price
def set_price(self, price):
self.price = price
def get_publisher(self):
return self.publisher
def set_publisher(self, publisher):
self.publisher = publisher
def display(self):
print("书名:", self.name)
print("出版日期:", self.publish_date)
print("作者:", self.author)
print("价格:", self.price)
print("出版社:", self.publisher)
```
调用示例:
```python
book = Book("Python编程从入门到实践", "2016-11-15", "Eric Matthes", 79.80, "人民邮电出版社")
book.display()
book.set_price(69.90)
book.display()
```
阅读全文