2017-04-25 2 views
1

私は動作するクラスを書いていますが、それは私が欲しいものを私に与えるものではありません。これは私のコードです。Python:retailitem class

class RetailItem(): 
    def __init__(self,ItemDesc,unit_Inv,Price): 
     self.ItemDesc=ItemDesc 
     self.unit_Inv=unit_Inv 
     self.Price=Price 
    def get_ItemDesc(self): 
     return self.ItemDesc 
    def get_unit_Inv(self): 
     return self.unit_Inv 
    def get_Price(self): 
     return self.Price 

def main(): 

    ItemDesc1=RetailItem('Jacket', '12', '59.95') 

    unit_Inv1=ItemDesc1.get_unit_Inv() 
    Price1=ItemDesc1.get_Price() 

    ItemDesc2=RetailItem('Designer Jeans', '40', '34.95') 
    unit_Inv2=ItemDesc2.get_unit_Inv() 
    Price2=ItemDesc2.get_Price() 


    ItemDesc3=RetailItem('Shirt', '20', '24.95') 
    unit_Inv3=ItemDesc3.get_unit_Inv() 
    Price3=ItemDesc3.get_Price() 


    print('Description Units In Inventory Price') 
    print('_________________________________') 
    print(ItemDesc1,unit_Inv1, Price1, sep=' ') 
    print(ItemDesc2,unit_Inv2,Price2,sep=' ') 
    print(ItemDesc3,unit_Inv3,Price3,sep=' ') 

main() 

それが私に与え出力は私の望ましい結果は次のよう

Description Units In Inventory Price 
_________________________________ 
<__main__.RetailItem object at 0x01AF6C10> 12 59.95 
<__main__.RetailItem object at 0x03BB8250> 40 34.95 
<__main__.RetailItem object at 0x04062510> 20 24.95 

をする必要がありますされています。私は間違って行くんでしたどこので、私の質問がある

Description Units In Inventory Price 
__________________________________________ 
Jacket   12     59.95 
Designer Jeans 40     34.95 
Shirt   20     24.95 

?実際の名前そのもののために別のコード行が必要かもしれないと私は考えました。しかし、私がそれをしたとき、それは私にtyperrorを与えました。いくつかの指針やアドバイスがいいでしょう。

+3

'__str__'メソッドを実装する必要があります。また、Pythonでは、プロパティ名は小文字で始まります。 – ozgur

+0

これは私に型エラー.. – CarryDove

+0

を与えます。あるいは 'ItemDesc1.ItemDesc'を' print'の最初の引数として出力します(しかし、あなたのクラスのメソッドとして '__str__'を実装することをお勧めします)。 –

答えて

0

strメソッドをクラスに追加してください。説明は、Pythonオブジェクトではなく文字列として返されます。

class RetailItem(): 
    def __init__(self,ItemDesc,unit_Inv,Price): 
     self.ItemDesc=ItemDesc 
     self.unit_Inv=unit_Inv 
     self.Price=Price 
    def __str__(self): 
     return self.ItemDesc 
    def get_ItemDesc(self): 
     return self.ItemDesc 
    def get_unit_Inv(self): 
     return self.unit_Inv 
    def get_Price(self): 
     return self.Price 

def main(): 

    ItemDesc1=RetailItem('Jacket', '12', '59.95') 
    ItemDesc2=RetailItem('Designer Jeans', '40', '34.95') 
    ItemDesc3=RetailItem('Shirt', '20', '24.95') 

    print('Description Units In Inventory Price') 
    print('_________________________________') 
    print(ItemDesc1, ItemDesc1.get_unit_Inv(), ItemDesc1.get_Price(), sep=' ') 
    print(ItemDesc2, ItemDesc2.get_unit_Inv(), ItemDesc2.get_Price(), sep=' ') 
    print(ItemDesc3, ItemDesc3.get_unit_Inv(), ItemDesc3.get_Price(), sep=' ') 

main() 
0

私は明らかにアイテムの説明に別の行を追加する必要がありました。

これは私が必要としていたものです。

ItemDesc03=ItemDesc3.get_ItemDesc()