具有屬性的圖書物件:標題、作者、年份
class Book():
def __init__(self, title = "", author = "", year = None):
self.title = title
self.author = author
self.year = year
def getTitle(self):
return self.title
def getAuthor(self):
return self.author
def getYear(self):
return self.year
def getBookDetails(self):
string = ("Title: {}, Author: {}, Year: {}"\
.format(self.title, self.author, self.year))
return string
名為 BookCollection 的鏈表
class BookCollection()
def __init__(self):
self.head = None
def insertBook(self, book)
temp = BookCollectionNode(book)
temp.setNext(self.head)
self.head = temp
def getAllBooksInCollection(self):
node = self.head
這是下面的問題。我使用 while 回圈列印每個節點,但是它列印節點的位置
while node:
print(node)
node = node.next
名為 BookCollectionNode 的節點類
class BookCollectionNode():
def __init__(self, data):
self.data = data
self.next = None
def getData(self):
return self.data
def getNext(self):
return self.next
def setData(self, newData):
self.data = newData
def setNext(self, newNext):
self.next = newNext
使用下面的函式列印鏈表的節點
b0 = Book("Cujo", "King, Stephen", 1981)
b1 = Book("The Shining", "King, Stephen", 1977)
b2 = Book("Ready Player One", "Cline, Ernest", 2011)
b3 = Book("Rage", "King, Stephen", 1977)
bc = BookCollection()
bc.insertBook(b0)
bc.insertBook(b2)
bc.insertBook(b3)
print(bc.getAllBooksInCollection())
uj5u.com熱心網友回復:
使固定
由于 node 是 a BookCollectionNode,您需要訪問它的book屬性,然后呼叫getBookDetails()方法
def getAllBooksInCollection(self):
node = self.head
while node:
print(node.data.getBookDetails())
node = node.next
并在bc.getAllBooksInCollection()沒有列印的情況下呼叫。由于沒有 return 陳述句,它回傳None您看到的列印內容
改進
似乎您并不真的需要訪問您的屬性的方法,但是如果您真的想要它們,請使用
@propertyclass Book: def __init__(self, title="", author="", year=None): self._title = title self._author = author self._year = year @property def title(self): return self._title @property def author(self): return self._author @property def year(self): return self._year覆寫方法
__str__以在列印時獲取內置字串class BookCollectionNode: def __str__(self): return str(self.data) class Book: def __str__(self): return f"Title: {self._title}, Author: {self._author}, Year: {self._year}"對于
BookCollectionNodegetter/setter,正確的方法是class BookCollectionNode: def __init__(self, data): self._data = data self._next = None @property def data(self): return self._data @property def next(self): return self._next @data.setter def data(self, newData): self._data = newData @next.setter def next(self, newNext): self._next = newNext
最后BookCollection班級變成
class BookCollection:
def __init__(self):
self.head = None
def insert_book(self, book):
temp = BookCollectionNode(book)
temp.next = self.head
self.head = temp
# 'print_' and not 'get_' as you don't return anything
def print_all_books_in_collection(self):
node = self.head
while node:
print(node)
node = node.next
uj5u.com熱心網友回復:
我認為,您需要在建構式中添加一個 data 引數才能得到如下內容:
class Node:
def __init__(self, data, next=None):
self.data = data
self.next = next
# Using an object of a Book with attributes: Title, Author, Year
b0 = Book("Cujo", "King, Stephen", 1981)
b1 = Book("The Shining", "King, Stephen", 1977)
b2 = Book("Ready Player One", "Cline, Ernest", 2011)
b3 = Book("Rage", "King, Stephen", 1977)
head = Node(b0, Node(b1, Node(b2, Node(b3))))
def print_list(head, end='\n'):
while head:
# You can extend the below logic
print(head.data, end=' -> ' if head.next else '')
head = head.next
print(end=end)
print_list(head)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/351918.html
上一篇:C 使用向量對讀取資料型別stringint和double
下一篇:將字串元素分配給變數
