Skip to main content
 首页 » 编程设计

python之从python中的另一个类访问自己

2025年01月19日12youxin

我目前正在学习 Thinkful Python 类(class),但我不知道如何在另一个类中使用一个类的 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 函数中。但我知道这不是正确的方法,但我想不出其他方法。

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

请您参考如下方法:

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

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

您还必须在 BicycleShop 上实现 def get_price(self, model) 方法,因为(再次关系)价格不仅取决于型号,还取决于商店出色地。然后它会像这样:

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