是否有必要在每次必须考虑新实例时更改方法



我是这个网站的新手,由于这个代码"有效",我正在寻找代码审查,但该方法有一个多余的打印语句。输出的格式正确,但打印语句可能完成了大部分工作,而不是代码?每次必须考虑新实例时都必须更改方法,这正常吗?";总计:";如果我只是添加了一个新实例(没有镜像方法中的更改),那么输出在这段代码中就不能正常工作。给出的示例输入是10瓶水,每瓶1美元,1袋巧克力片,每袋3美元。

class ItemToPurchase:

def __init__(self, item_name = 'none', item_price = 0, item_quantity = 0):
self.item_name = item_name
self.item_price = item_price
self.item_quantity = item_quantity        
def print_item_cost(self, item_name):
print('{} {} @ ${} = ${}'.format(item_name, item_quantity, item_price, (item_price * item_quantity)))
print('{} {} @ ${} = ${}'.format(item_name2, item_quantity2, item_price2, (item_price2 * item_quantity2)))

if __name__ == "__main__":

# Type main section of code here
item_one = ItemToPurchase()
print('Item 1')
item_name = input('Enter the item name:n')
item_price = int(input('Enter the item price:n'))
item_quantity = int(input('Enter the item quantity:n'))

item_two = ItemToPurchase(item_name, item_price, item_quantity)
print()
print('Item 2')
item_name2 = input('Enter the item name:n')
item_price2 = int(input('Enter the item price:n'))
item_quantity2 = int(input('Enter the item quantity:n'))

print()
print('TOTAL COST')
item = ItemToPurchase()
item.print_item_cost(item_name)
print()
print('Total: $' + str((item_price * item_quantity) + (item_price2 * item_quantity2)))

self参数是该类的任何实例的替身。它保存关于由__init__方法实例化的对象的给定实例的所有信息。self参数仅对类的单个实例可见,因此item_1item_2都可以引用self,并且只能获得有关它们自己的信息。

对于打印对象,我建议实现__str__,它允许您简单地调用print(instance_of_my_class):

class ItemToPurchase:
def __init__(self, item_name = 'none', item_price = 0, item_quantity = 0):
self.item_name = item_name
self.item_price = item_price
self.item_quantity = item_quantity
self.total_cost = self.item_price * self.item_quantity   
def __str__(self):
return f"{self.item_name} {self.item_quantity} @ {self.item_price:.2f} = ${self.total_cost:.2f}"

请注意f字符串的使用,这使您更容易阅读和理解格式化字符串应该做什么。还请注意价格和新属性self.total_cost的格式代码.2f的使用(这将正确显示您的价格,例如,它将返回$21.00,而不是$21.0)。

用法:

item = ItemToPurchase('ping pong paddle', 5.25, 4)
print(item)

输出:

ping pong paddle 4 @ 5.25 = $21.00

您似乎误解了类的用途。类的每个实例都表示该类的单个对象,其方法应该只关心该对象。因此,print_item_cost应该只打印本身的成本,并且它应该从self参数中获得所需的所有信息,该参数表示方法所属的对象。

class ItemToPurchase:
def __init__(self, item_name = 'none', item_price = 0, item_quantity = 0):
self.item_name = item_name
self.item_price = item_price
self.item_quantity = item_quantity        
def print_item_cost(self):
print('{} {} @ ${} = ${}'.format(self.item_name, self.item_quantity, self.item_price, (self.item_price * self.item_quantity)))

请注意,print_item_cost不需要item_name参数,因为它从self获取项目名称。

然后,您可以创建任意数量的类实例,并打印每个实例的信息:

if __name__ == "__main__":
print('Item 1')
item_name = input('Enter the item name:n')
item_price = int(input('Enter the item price:n'))
item_quantity = int(input('Enter the item quantity:n'))
# Create the item using the inputs you took
item_one = ItemToPurchase(item_name, item_price, item_quantity)
print()
print('Item 2')
item_name2 = input('Enter the item name:n')
item_price2 = int(input('Enter the item price:n'))
item_quantity2 = int(input('Enter the item quantity:n'))
item_two = ItemToPurchase(item_name2, item_price2, item_quantity2)
item_one.print_item_cost()
item_two.print_item_cost()

理想情况下,您可以将其循环使用,并使用列表来跟踪项目,但这是另一个问题。

最新更新