2017-10-11 23 views
1

私は現時点でThinkful Pythonコースをやっているので、別のクラスのあるクラスのself属性の使い方を理解できません。Pythonの別のクラスから自分自身にアクセスする

class Bicycle(object): 
    # Have a model name 
    # Have a weight 
    # Have a cost to produce 
    def __init__(self, model): 
     self.model = model 
     pass 


class BicycleShop(object): 
    # Create a bicycle shop that has 6 different bicycle models in stock. The shop should charge its customers 20% over the cost of the bikes 
    margin = 1.2 
    # Have a name 
    # Have an inventory of different bikes 
    # Sell bikes with a margin over their cost 
    # Can see a total of how much profit they have made 
    def __init__(self, company_name, models): 
     self.company_name = company_name 
     self.models = models 

    def bicycle_models(self): 
     for model in self.models.keys(): 
      print(model) 

    def bicycle_prices(self): 
     for model, price in self.models.items(): 
      if price <= customer_1.budget: 
       print("The {} is available for a price of ${:.2f}.".format(model, price * self.margin)) 


class Customer(object): 
    # Have a name 
    # Have a fund of money used to purchase the bike 
    # Can buy and own a new bicycle 
    def __init__(self, name, budget): 
     self.name = name 
     self.budget = budget 

    def check_funds(self): 
     return evans_cycles.bicycle_prices() 



evans_cycles = BicycleShop("Evans Cycles", { "BMC Road Machine": 125, "Cannondale Synapse": 275, "Pinnacle Laterite": 450, "Fuji Transonic": 625, "Cervelo R2": 750, "Specialized Roubaix": 999 }) 
print("\nWe are {} Bicycle Shop. Please see our range of bikes, below.\n".format(evans_cycles.company_name)) 
evans_cycles.bicycle_models() 

customer_1 = Customer('Stuart', 1000) 
print("\nHello, I'm {} and my budget is ${}. What can I afford?\n".format(customer_1.name, customer_1.budget)) 

print(customer_1.check_funds()) 

現在、私はハードcheck_funds関数にbicycle_prices方法とevans_cyclesにcustomer_1.budgetでコーディングされています。しかし、私はこれが正しい方法ではないことを認識していますが、他の方法でこれを行う方法を理解することはできません。

あるクラスの属性を別のクラスで利用する正しい方法は何ですか?私は継承を使用しようとしましたが、うまくいきませんでした。

+0

「私は継承を使用しようとしています。継承は「is-a」関係とみなすことができます。たとえば、自転車の基本的な動作をすべてカバーする自転車のクラスがありますが、マウンテンバイク(「マウンテンバイクは*バイク」)など、特定の種類の自転車にいくつかの追加の動作を実装する場合は、 Bicycleクラスの動作を継承するMountainBikeクラスを作成できます。 – Jurgy

答えて

1

何かを設計するときはいつでも、関係について考える必要があります。では、顧客はどのように店に関係していますか?さて、各ショップには顧客があり、各顧客には1つのショップしかないと仮定することができます(例として真実である必要はありません)。その場合はあなたがするでしょう

class BicycleShop: 
    ... 

class Customer: 
    def __init__(self, shop): 
     self.shop = shop 

ですので、お客様はショップへの参照があります。価格だけでなく、依存する(再び関係)ので、あなたはまた、BicycleShopdef get_price(self, model)メソッドを実装する必要が

class Customer: 
    def __init__(self, name, budget, shop): 
     self.name = name 
     self.shop = shop 
     self.budget = budget 

    def check_funds(self): 
     models = self.shop.get_models() 
     for model, price in models.items(): 
      if price <= self.budget: 
       print("The {} is available for a price of ${:.2f}.".format(model, self.get_price(model))) 

Customer

class BicycleShop: 
    def get_models(self): 
     return self.models 

し、最終的にcheck_funds:今、あなたは、ショップでget_models()機能を公開することができますモデルだけでなく、店にも。それで、これは次のようになります:

evans_cycles = BicycleShop("Evans Cycles", { "BMC Road Machine": 125, "Cannondale Synapse": 275, "Pinnacle Laterite": 450, "Fuji Transonic": 625, "Cervelo R2": 750, "Specialized Roubaix": 999 }) 
print("\nWe are {} Bicycle Shop. Please see our range of bikes, below.\n".format(evans_cycles.company_name)) 

customer_1 = Customer('Stuart', 1000, evans_cycles) 
customer_1.check_funds() 
関連する問題