从python中的另一个类访问self

时间:2017-10-11 10:33:55

标签: python

我正在做Thinkful Python课程,我无法弄清楚如何在另一个类中使用一个类的self属性。

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())

目前,我已将customer_1.budget硬编码到bicycle_prices方法中,并将evans_cycles硬编码到check_funds函数中。但我知道这不是正确的做法,但我无法弄清楚如何以其他方式做到这一点。

在另一个类中使用一个类的属性的正确方法是什么?我试图使用继承,但它不起作用,它不会接受我的字典作为我认为的参数。

1 个答案:

答案 0 :(得分:1)

每当你设计一些东西时,你必须考虑关系。那么客户如何与商店相关?好吧,我们假设每个商店都有客户,每个客户只有一个商店(不一定是真的,仅作为一个例子)。在那种情况下你会做

class BicycleShop:
    ...

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

所以现在客户可以参考商店。现在,您可以在商店中公开get_models()功能:

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

以及check_funds上的Customer

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)))

您还必须在def get_price(self, model)上实施BicycleShop方法,因为(再次关系)价格不仅取决于模型,还取决于商店。那就是这样的:

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()