【问题标题】:Python 3.0 Classes and OOP basics, with UML model?Python 3.0 类和 OOP 基础,带有 UML 模型?
【发布时间】:2015-12-18 13:59:03
【问题描述】:

我正在尝试制作一个程序来锻炼我在我创建的模块中的课程和格式/基本数学知识。这个想法是创建一个Merchandise 数据类(称为Merchandise.py),并在一个单独的文件中创建一个名为store_merch.py 的可执行程序来测试它。我知道我应该像这样在基本的 UML 图之后对其进行建模:

Merchandise

__item

__quantity

__cost

__init__(item,quantity,cost)

set_item(item)

set_quantity(quantity)

set_cost(cost)

get_item()

get_quantity()

get_cost()

get_inventory_value()

__str__()

我知道get_inventory_value() 方法将以货币格式计算/打印商品的价值,__str()__ 方法将返回一个字符串,显示所有 3 个属性和库存值(数量 * 成本)。

编辑:我在这里问的问题似乎有些混乱,所以我在下面强调了我所得到的核心概念:(注意,我试图使用 getter/setter 强调)。这里有两个程序,商品.py 包含所有的 getter/setter,第二个程序要求用户输入并运行商品.py 模块

对于store_merch 程序,我想:

为 10 个锤子创建一个 Merchandise 实例,每个成本为 14.95 美元。

为 6 条项链创建另一个 Merchandise 对象,每条售价 799.99 美元。

运行两个对象的__str__ 方法。

提示用户输入锤子对象的新数量,然后使用 setter 方法更改此属性。

提示用户输入项链对象的新成本,然后使用 setter 方法更改此属性。

通过再次运行 __str__ 方法来验证更改。

同时显示每个对象的库存值。

样本输出

锤子,10 个 @ 14.95 美元

项链,6 美元,799.99 美元

输入硬件 12 的新数量

输入珠宝 659 的新成本

锤子,12 美元,14.95 美元

库存价值:179.40 美元

项链,6 美元,659.00 美元

库存价值:3,954.00 美元

到目前为止我有两个程序如下:

 ## first program "merchandise.py" with getters/setters to use in next program:

class Merchandise:
    def __init__(self,item,quantity,cost):
        self.__item = item
        self.__quantity = quantity
        self.__cost = cost

    def set_item(self,item):
        self.__item = item

    def set_quantity(self,quantity):
        self.__quantity = quantity

    def set_cost(self,cost):
        self.__cost = cost

    def get_item(self):
        return self.__item

    def get_quantity(self):
        return self.__quantity

    def get_cost(self):
        return self.__cost

    def __str__(self):
        s = self.__item + ' ' + str(self.__quantity)
        s += ' @ ' + str(self.__cost)
        return s


## --------SEPARATE PROGRAM TO RUN MERCHANDISE MODULE BELOW----------

## merchandise input program
### import the merchandise module
import merchandise

def main():
    ### make a new merchandise? instance
    store_merchandise = merchandise.Merchandise('hammer',10,14.95)
    ### display status of merchandise object
    print(store_merchandise)

    ### change Item, Quantity, and Cost
    store_merchandise.set_item('necklaces')
    store_merchandise.set_quantity(6)
    store_merchandise.set_cost(799.99)
    ### display status of store merchandise object
    print(store_merchandise)

    item2 = input('Enter new quantity for hardware ') # prompt user for new quantity
    ### initiate another merchandise
    ### change Item, Quantity, and Cost
    store_merchandise.set_item('hammer')
    store_merchandise.set_quantity(item2)
    store_merchandise.set_cost(14.95)
    ### display status of store merchandise object
    print(store_merchandise)

    necklace2 = input('Enter new cost for necklaces ') # prompt user for new quantity
    ### initiate another merchandise
    ### change Item, Quantity, and Cost
    store_merchandise.set_item('necklace')
    store_merchandise.set_quantity(necklace2)
    store_merchandise.set_cost(659.00)
    ### display status of store merchandise object
    print(store_merchandise)


    ### add the new inventory value total and print the sum

    print(store_merchandise + format(store_merch.get_cost(), ',.2f'), sep='')

main()

目前它只重命名锤子实例及其属性并正确提示用户,打印锤子和项链的新价格/数量,但不是我想要的方式。 我的一个朋友帮助指出了我做错的一些事情,但老实说,我对如何创建更多实例并调用以前的实例感到有点茫然,同时将每个实例的属性添加在一起并显示结果:

1.您没有为项链创建第二个对象。您重新分配了硬件对象的属性。你没有使用输入。 这... store_merchandise.set_quantity(12) 应该是... store_merchandise.set_quantity(item2)

2。在您输入新的锤子数量时,您忘记使用 int() 函数。

3.以 store_merch = 开头的行是另一个罪魁祸首。您忘记使用商品。商品,就像您在 main 开始时所做的那样。但无论如何,您不应该创建一个名为 store_merch 的新实例。您应该使用 store_merchandise 对象的设置器来设置数量。从那里拿走!

感谢您的任何帮助!第一个问题在这里,所以我希望我在发布之前遵守所有规则! (:

【问题讨论】:

  • 您的具体问题是什么?
  • python中不需要getter和setter,因为可以使用@property:python-course.eu/python3_properties.php
  • 我正在尝试获取代码上方的示例输出,并且我知道属性功能,但这个练习专门用于使用 getter/setter 管理和编辑类模块,因为我很清楚需要审查才能让他们工作。
  • 我非常困惑。我不明白问题是什么。
  • "class module" 没有任何意义...而且这里似乎没有人清楚地了解您的问题到底是什么。

标签: python class loops math


【解决方案1】:

我不太确定你的问题是什么,但我知道你朋友所说的“你没有为项链创建第二个对象”是什么意思

您将 store_merchandise 作为商品对象启动,然后不断使用 set_x(x) 更改其属性。

相反,您应该通过初始化另一个变量来实例化对象的新实例,而不是更改原来的属性。这可以在您启动第一个相同的情况下完成,但使用不同的名称。

store_merchandise = merchandise.Merchandise('hammer',10,14.95) //initiate the first object properties 'hammer',12,14.95
store_merchandise2 = merchandise.Merchandise('necklaces',6,799.99) //initiate the second object with properties 'necklaces',6,799.99

然后您可以像以前一样访问它们,在需要时分别调用它们。这样,您就有了 Merchandise 对象的两个单独实例,而不是更改单个实例。

【讨论】:

  • 非常感谢 R Nar 我没有意识到我没有创建对象的新实例,这是迄今为止最大的帮助:)
猜你喜欢
  • 1970-01-01
  • 2012-02-04
  • 2012-05-16
  • 1970-01-01
  • 2011-09-28
  • 2011-07-05
  • 2014-06-19
  • 2012-05-28
  • 1970-01-01
相关资源
最近更新 更多