亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python 新手:嘗試從列表中打印對象屬性

Python 新手:嘗試從列表中打印對象屬性

開心每一天1111 2022-06-02 10:37:34
import ShoppingClass as SCclass ShoppingCartPrinter:    shoppingCart = SC.ShoppingCart()    while(True):        item = SC.ItemToPurchase()        item.setName(input('\nEnter Name: '))        item.setDescription(input('\nSet Description: '))        item.setPrice(input('\nSet price: '))        item.setQuantity(input('\nSet Quantity: '))        shoppingCart.addItem(item)        for shoppingCart.cartItems.getName() in shoppingCart.cartItems:            print(shoppingCart.cartItems.getName())我正在嘗試自學來自 java 的 python 并編寫了這個簡單的程序。我有另一個類,我用它來創建對象“項目”和“購物車”。在購物車的構造函數中,我創建了一個空列表,向其中添加對象。如果我嘗試打印對象的屬性,我不知道語法應該如何。我上面的內容顯然是錯誤的。抱歉,如果這是一個簡單的答案,我們將不勝感激。謝謝class ItemToPurchase:    def __init__(self,itemName ='none',itemDescription = 'none',itemPrice = 0,itemQuantity = 0):    self.itemName = itemName    self.itemDescription = itemDescription    self.itemPrice = itemPrice    self.itemQuantity = itemQuantitydef setName(self,newName):    self.itemName = newNamedef getName(self):    return self.itemNamedef setPrice(self, newPrice):    self.itemPrice = newPricedef getPrice(self):    return self.itemPricedef setQuantity(self, newQuantity):    self.itemQuantity = newQuantitydef getQuantity(self):    return self.itemQuantitydef setDescription(self,description):    self.itemDescription = descriptiondef getDescription(self):    return self.itemDescriptiondef printItemCost():    print(itemName+" "+itemQuantity+" @ $"+itemPrice+" = "+itemPrice*itemQuantity)def printItemDescription():    print(itemName+" "+itemDescription)class ShoppingCart:    def __init__(self,customerName = 'none', currentDate = 'January 1, 2016'):        self.customerName = customerName        self.currentDate = currentDate        self.cartItems = []    def addItem(self, item):        self.cartItems.append(item)    def getDate(self):        return self.currentDate    def getCustomerName(self):        return self.customerName
查看完整描述

3 回答

?
躍然一笑

TA貢獻1826條經驗 獲得超6個贊

如果沒有看到其他班級,我肯定不能說任何話。但我認為問題可能是由承包商引起的。我看到的是您沒有將任何值傳遞給構造函數,但您正在嘗試使用 setter 函數設置它們的屬性。如果您希望其他人分配它,只需執行以下操作:


name = input("Enter name")

description = input("Enter description")

item = foo(name, description)

使用這種模式來分配其他用戶鍵入的變量。對于打印部分,請使用它。


for i in shoppingCart.cartItems:

    print(i.getName())

如果您有任何問題隨時問。希望能幫助到你 :)


PS:當你更新時,我正在寫我的答案。我的回答仍然很重要。我認為你過于復雜了。


查看完整回答
反對 回復 2022-06-02
?
HUWWW

TA貢獻1874條經驗 獲得超12個贊

您的代碼的一個問題是在 ItemToPurchase 類中過度使用了 setter 函數。用構造函數設置值更好嗎?Java 課程經常教導您需要為所有屬性構建 getter 和 setter,但這不是最好的主意,并且會使代碼變得臃腫。只有在絕對需要的情況下才應該實施它們(在您的情況下,只需要更改 itemQuantity ,并且可能需要更改 itemPrice 以獲取優惠券或類似的東西)。此外,在 Python 中,您可以返回所有 ItemToPurchase 屬性的元組,而不是使用單獨的 getter:


def getAttributes(self):

    return (self.itemName, self.itemDescription, self.itemPrice, self.itemQuantity)

然后你不需要在你的主類/主函數中返回所有這些訪問,你只需分配給局部變量并以這種方式構造 ItemToPurchase。然后你有更少的類訪問/函數調用,它更干凈。


另一個問題是 ShoppingCart 是如何構建的。雖然 Python 并不完全具有 Java 或 C++ 之類的“私有”屬性,但您可能希望在該類的 __init__ 中將 cartItems 列表設為“私有”(已損壞,但嘿,足夠接近),并構建一個檢索器:


    self.__cartItems = []


def getCartSize():

    return len(self.__cartItems)


def getCartItem(pos):

    return self.__cartItems[pos].getAttributes()

然后恢復 ShoppingCart 中第一個 ItemToPurchase 的屬性:


myItem = getCartItem(0)  #returns a tuple of all the data about that ItemToPurchase

print(myItem[0])         #prints the name of that item in the cart

Python 類和 Java 類之間最大的區別可能是“私有”概念的實現方式,以及 Python 返回語句的更大靈活性。


查看完整回答
反對 回復 2022-06-02
?
慕田峪9158850

TA貢獻1794條經驗 獲得超7個贊

您發布的代碼有很多問題,但是打印項目的循環應該如下所示:

for item in shoppingCart.cartItems:
    print(item.getName())


查看完整回答
反對 回復 2022-06-02
  • 3 回答
  • 0 關注
  • 111 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號