第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

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())我正在嘗試自學(xué)來自 java 的 python 并編寫了這個(gè)簡單的程序。我有另一個(gè)類,我用它來創(chuàng)建對象“項(xiàng)目”和“購物車”。在購物車的構(gòu)造函數(shù)中,我創(chuàng)建了一個(gè)空列表,向其中添加對象。如果我嘗試打印對象的屬性,我不知道語法應(yīng)該如何。我上面的內(nèi)容顯然是錯(cuò)誤的。抱歉,如果這是一個(gè)簡單的答案,我們將不勝感激。謝謝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貢獻(xiàn)1826條經(jīng)驗(yàn) 獲得超6個(gè)贊

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


name = input("Enter name")

description = input("Enter description")

item = foo(name, description)

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


for i in shoppingCart.cartItems:

    print(i.getName())

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


PS:當(dāng)你更新時(shí),我正在寫我的答案。我的回答仍然很重要。我認(rèn)為你過于復(fù)雜了。


查看完整回答
反對 回復(fù) 2022-06-02
?
HUWWW

TA貢獻(xiàn)1874條經(jīng)驗(yàn) 獲得超12個(gè)贊

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


def getAttributes(self):

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

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


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


    self.__cartItems = []


def getCartSize():

    return len(self.__cartItems)


def getCartItem(pos):

    return self.__cartItems[pos].getAttributes()

然后恢復(fù) ShoppingCart 中第一個(gè) 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 類之間最大的區(qū)別可能是“私有”概念的實(shí)現(xiàn)方式,以及 Python 返回語句的更大靈活性。


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

TA貢獻(xiàn)1794條經(jīng)驗(yàn) 獲得超8個(gè)贊

您發(fā)布的代碼有很多問題,但是打印項(xiàng)目的循環(huán)應(yīng)該如下所示:

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


查看完整回答
反對 回復(fù) 2022-06-02
  • 3 回答
  • 0 關(guān)注
  • 117 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號