问题相关代码,请勿粘贴截图
class Person:
def __init__(self, name, age):
self.__names=name
self.__age=age
def __str__(self):
return "Person: Name='"+" ".join(self.__names)+"', Age="+repr(self.__age)
def get_full_name(self):
return " ".join(self.__names)
def bithday(self):
self.__age += 1
def get_age(self):
return self.__age
def get_name_harvard_format(self):
initial=[]
for word in self.__names:
for letter in word:
if letter.isupper():
initial.append(letter)
del initial[-1]
return self.__names[-1],"".join(initial)
class Book:
def __init__(self, authors, title, publisher, publishYear, publishPlace):
self.__authors=authors
self.__title=title
self.__publisher=publisher
self.__publishYear=publishYear
self.__publishPlace=publishPlace
def display_author(self):
print(self.__authors)
def get_harvard_reference(self):
self.__authors.get_name_harvard_format()
return "".join(self.__authors),f'{self.__publishYear}, {self.__title}, {self.__publisher}, {self.__publishPlace}'
def __str__(self):
return self.get_harvard_reference()
Output
programmer = Person(['Augusta', 'Ada', 'King'], 36)
engineer = Person(['Charles', 'Babbage'], 50)
actualAuthor = Person(['Sydney','Padua'], 30)
book = Book([programmer, engineer, actualAuthor],
'The Thrilling Adventures of Lovelace and Babbage',
'Penguin Books', 2015, 'Westminster')
print(book)
book.display_author()
运行结果及报错内容
Traceback (most recent call last):
File "c:\Users\rongt\OneDrive - University of South Australia\OOP\WEEK5\practical.py", line 62, in
print(book)
File "c:\Users\rongt\OneDrive - University of South Australia\OOP\WEEK5\practical.py", line 53, in str
return self.get_harvard_reference()
File "c:\Users\rongt\OneDrive - University of South Australia\OOP\WEEK5\practical.py", line 50, in get_harvard_reference
self.__authors.get_name_harvard_format()
AttributeError: 'list' object has no attribute 'get_name_harvard_format'
我的解答思路和尝试过的方法
导进class Person然后得到来自class Person.get_name_harvard_format的返回到class Book.get_harvard_reference
这里是两道题, Person是第一道题答案做出来的,所以并为考虑如何实现Book的答案
我想要达到的结果
King, AA, Babbage, C & Padua, S 2015, The Thrilling Adventures of Lovelace and
Babbage, Penguin Books, Westminster.
[King, AA, Babbage, C, Padua, S]