定义一个类用来表示书籍,有书名,作者,出版社,价格,关键字等属性,获取第一个关键字,获取作者数等方法,再定义一个教辅书类来继承书籍类,新增加教辅科目,年级等属性
时间: 2024-05-20 07:17:44 浏览: 104
class Book:
def __init__(self, title, author, publisher, price, keywords):
self.title = title
self.author = author
self.publisher = publisher
self.price = price
self.keywords = keywords
def get_first_keyword(self):
return self.keywords.split(',')[0]
def get_author_count(self):
return len(self.author.split(','))
class Textbook(Book):
def __init__(self, title, author, publisher, price, keywords, subject, grade):
super().__init__(title, author, publisher, price, keywords)
self.subject = subject
self.grade = grade
book = Book('Python for Beginners', 'John Smith', 'ABC Publishing', 19.99, 'Python,Programming')
print(book.get_first_keyword()) # Python
print(book.get_author_count()) # 1
textbook = Textbook('Mathematics for Elementary School', 'Jane Doe', 'XYZ Publishing', 29.99, 'Math,Education',
'Mathematics', 'Elementary School')
print(textbook.get_first_keyword()) # Math
print(textbook.get_author_count()) # 1
阅读全文