私は現時点で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でコーディングされています。しかし、私はこれが正しい方法ではないことを認識していますが、他の方法でこれを行う方法を理解することはできません。
あるクラスの属性を別のクラスで利用する正しい方法は何ですか?私は継承を使用しようとしましたが、うまくいきませんでした。
「私は継承を使用しようとしています。継承は「is-a」関係とみなすことができます。たとえば、自転車の基本的な動作をすべてカバーする自転車のクラスがありますが、マウンテンバイク(「マウンテンバイクは*バイク」)など、特定の種類の自転車にいくつかの追加の動作を実装する場合は、 Bicycleクラスの動作を継承するMountainBikeクラスを作成できます。 – Jurgy