2017-01-31 9 views
-3
class Coffee: 
    def __init__(self): 
     self._price=4.0 

    def price(self): 
     return self._price 


    def __str__(self): 
     return "Coffee with price "+ str(self._price) 


class CoffeeWithMilk: 
    def __init__(self, coffee): 
     self.price+=coffee.price+0.5 

    def price(self): 
     return self.price 


coffee=Coffee() 

x=CoffeeWithMilk(coffee) 
coffeeWithMilk=CoffeeWithMilk(x) 
print(coffeeWithMilk) 

これを修正するにはどうすればよいですか?ありがとう+: 'method'と 'float'のサポート対象外のオペランドタイプ

+0

最終ではなく、少なくとも、あなたはCoffeeインスタンスからCoffeeWithMilkインスタンスを作成し、その後、最初CoffeeWithMilkのインスタンスから別のCoffeeWithMilkのインスタンスは、ので、あなたの最後のインスタンスが0.5二回4に追加されましたなぜあなたはその場にいるのですか?また、 'price'はメソッドであることがわかります –

+0

self.price = coffee.price()+ 0.5 –

答えて

-1

coffee.priceの方法ですので、coffee.price + 0.5はエラーです。

あなたが代わりにそのメソッドの結果を取得したい場合は、呼び出し方法:

self._price = coffee.price() + 0.5 

注、私はあなたがすべての後に新しい属性を設定している、ここで=+=を置き換えます。私もの名前をに変更しました。それ以外の場合はCoffeeWithMilk.priceメソッドが実際に混乱し、の第2ののエラーが発生します。self.priceもまだ方法です。

def price(self): 
    return self._price 

ので、完成したコードは次のようになります:これはdef price(self)メソッドへの固定必要

class Coffee: 
    def __init__(self): 
     self._price = 4.0 

    def price(self): 
     return self._price 

    def __str__(self): 
     return "Coffee with price " + str(self._price) 


class CoffeeWithMilk: 
    def __init__(self, coffee): 
     self._price = coffee.price() + 0.5 

    def price(self): 
     return self._price 

あなたが再定義クラスの継承を使用して完全にprice方法を避けることができます。最終的なprint(coffeeWithMilk)意志出力何か、もう少し面白いようにするには、あまりにも沿っ__str__実装を取得

class Coffee: 
    name = 'Coffee' 

    def __init__(self): 
     self._price = 4.0 

    def price(self): 
     return self._price 

    def __str__(self): 
     return "{} with price {}".format(self.name, self._price) 


class CoffeeWithMilk(Coffee): 
    name = 'Coffee with milk' 

    def __init__(self, coffee): 
     self._price = coffee.price() + 0.5 

CoffeeCoffeeWithMilk特殊なバージョンを作ります。

Coffee.pricepropertyにすることもできます。プロパティは、属性にアクセスするたびに自動的に呼び出されるメソッドです。

class Coffee: 
    name = 'Coffee' 

    def __init__(self): 
     self._price = 4.0 

    @property 
    def price(self): 
     return self._price 

    def __str__(self): 
     return "{} with price {}".format(self.name, self._price) 


class CoffeeWithMilk(Coffee): 
    name = 'Coffee with milk' 

    def __init__(self, coffee): 
     self._price = coffee.price + 0.5 

この場合、メソッドまたはプロパティは使用しません。ここに_priceを隠す必要はありません。

class Coffee: 
    name = 'Coffee' 

    def __init__(self): 
     self.price = 4.0 

    def __str__(self): 
     return "{} with price {}".format(self.name, self.price) 


class CoffeeWithMilk(Coffee): 
    name = 'Coffee with milk' 

    def __init__(self, coffee): 
     self.price = coffee.price + 0.5 

方法も財産もないが_price属性に沿って渡すより多くの何もしているためです:ちょうど直接属性に置き換えます。あなたはそれに直接アクセスすることもできます。

>>> coffee = Coffee() 
>>> x = CoffeeWithMilk(coffee) # first instance, from coffee 
>>> print(x) 
Coffee with milk with price 4.5 
>>> coffeeWithMilk = CoffeeWithMilk(x) # second instance, from x 
>>> print(coffeeWithMilk) 
Coffee with milk with price 5.0 
関連する問題