Python 如何在派生类中使用新信息覆盖基类方法?
我被问到以下问题: 给定基类书籍,定义一个名为Encyclopedia的派生类。在派生的百科全书类中,定义一个print_info()方法,该方法通过不仅打印标题、作者、出版商和出版日期,而且打印版本和卷数来覆盖图书类的print_info()方法 例如: 如果输入Python 如何在派生类中使用新信息覆盖基类方法?,python,Python,我被问到以下问题: 给定基类书籍,定义一个名为Encyclopedia的派生类。在派生的百科全书类中,定义一个print_info()方法,该方法通过不仅打印标题、作者、出版商和出版日期,而且打印版本和卷数来覆盖图书类的print_info()方法 例如: 如果输入 Enter Book Information Enter title: The Hobbit Enter author: J. R. R. Tolkien Enter publisher: George Allen & Un
Enter Book Information
Enter title: The Hobbit
Enter author: J. R. R. Tolkien
Enter publisher: George Allen & Unwin
Enter publication date: 21 September 1937
Enter title: The Illustrated Encyclopedia of the Universe
Enter Encyclopedia Information
Enter author: James W. Guthrie
Enter publisher: Watson-Guptill
Enter publication date: 2001
Enter edition: 2nd
Enter number of volumes: 1
输出
Book Information:
Book Title: The Hobbit
Author: J. R. R. Tolkien
Publisher: George Allen & Unwin
Publication Date: 21 September 1937
Encyclopedia Information:
Book Title: The Illustrated Encyclopedia of the Universe
Author: James W. Guthrie
Publisher: Watson-Guptill
Publication Date: 2001
Edition: 2nd
Number of Volumes: 1
我试过搞乱它,但我只是一直打印两次书的信息?
这是我的密码
class Book:
def __init__(self, title, author, publisher, publication_date):
self.title = title
self.author = author
self.publisher = publisher
self.publication_date = publication_date
def print_info(self):
print('Book Information:')
print(' Book Title:', self.title)
print(' Author:', self.author)
print(' Publisher:', self.publisher)
print(' Publication Date:', self.publication_date)
class Encyclopedia(Book):
# TODO: Define constructor with attributes:
# title, author, publisher, publication_date, edition, num_volumes
def __init__(self, title, author, publisher, publication_date, edition, num_volumes):
Book.__init__(self, title, author, publisher, publication_date)
self.edition = edition
self.num_volumes = num_volumes
# TODO: Define a print_info() method that overrides the print_info()
# in the Book class
def print_info(self):
print('Encyclopedia Information:')
print(' Book Title:', self.title)
print(' Author:', self.author)
print(' Publisher:', self.publisher)
print(' Publication Date:', self.publication_date)
print(' Edition:', self.edition)
print(' Number of Volumes:', self.num_volumes)
if __name__ == "__main__":
print('Enter Book Information')
title = input('Enter title: ')
author = input('Enter author: ')
publisher = input('Enter publisher: ')
publication_date = input('Enter publication date: ')
print('\nEnter Encyclopedia Information')
e_title = input('Enter title: ')
e_author = input('Enter author: ')
e_publisher = input('Enter publisher: ')
e_publication_date = input('Enter publication date: ')
edition = input('Enter edition: ')
num_volumes = int(input('Enter number of volumes: '))
#TODO: Creat a book instance and call the print_info method
book = Book(title, author, publisher, publication_date)
book.print_info()
#TODO: Creat a encyclopedia instance and call the print_info method
enc = Encyclopedia(title, author, publisher, publication_date, edition, num_volumes)
enc.print_info()
创建
enc
对象时,传递的参数与book
对象相同。它应该是enc=Encyclopedia(e_title,e_author,e_publications,e_publication,e_date,edition,num_volumes)
。只需将参数更改为:
因为您为这两个类发送相同的参数,所以它会将它们打印两次 它看起来像这样的原因是您没有使用属性定义所有属性。看起来您仍然缺少作者、出版商和发布日期。同时删除第20行。我想那应该是第20行,但这是你有书的地方。init(自我、标题、作者、出版商、出版日期)。删除整行代码,然后只定义author、publisher和publication\u date的属性值,就像对edition和num\u volume所做的那样,然后就可以了。其他一切看起来都很好
enc = Encyclopedia(e_title, e_author, e_publisher, e_publication_date, edition, num_volumes)
enc.print_info()