如何在不同 类 的属性之间组合(或进行操作)(尚未指定实例)?
How can I combine (or make operations) between attributes of different classes (without specifying instances yet)?
首先,我是一个新手。感谢您的耐心等待。
我正在设计一款软件来计算不同房屋的绝缘 materials 和数量。
我有一个 class House()
,它包含 roof_area 和 wall_area 等属性。
我有一个 class Insulator()
,它包含厚度和面积(打包 material 覆盖的区域)等属性
现在我想知道我应该购买多少包的绝缘子才能覆盖整个屋顶区域。
因此,操作将是:
House.roof_area / Insulator.area = insulator_packages_needed_for_roof
问题是我无法执行该操作:
AttributeError: type object 'House' has no attribute 'roof_area'.
当然我可以在一个实例范围内完成它,但我还不想指定一个实例,因为这个操作应该对将来构建的 Class 的任何实例完成.我应该使用继承吗?我的感觉是,Insulator和House是完全不同的东西,不应该继承混用,但我只是初学者
如果您想在不创建实例的情况下使用属性,您应该使用 class 个属性。
class House(object):
roof_area = 10 # Or whatever you see fit.
roof_surface = 20
class Insulator(object):
thickness = 10 # Or whatever you see fit.
surface = 20
这样您就可以通过键入 'House.roof_area' 来访问属性。
虽然,我不明白为什么你不能创建一个实例。它会防止在 class 属性中进行硬编码,并且在您的情况下会容易得多。
此外,您的操作语法无效,但也许您只是展示了伪代码。正确的语法是:
insulator_packages_needed_for_roof = House.roof_area / Insulator.area
在不使用 House
或 Insulator
classes。只有每个实例都有一个实例才有意义。
但是,您可以在创建实例之前编写代码来进行计算。只需将它放在一个将实例作为参数的函数中:
def roof_insulation_packages(house, insulator): # args are instances of House and Insulator
return house.roof_area / insulator.area # do the calculation and return it
将函数作为 classes 之一的方法可能更有意义。我什至建议 Insulator
实例可能是 House
实例属性的一个很好的候选者。那看起来像这样:
class House():
def __init__(self, roof_area, roof_insulator, wall_area, wall_insulator):
self.roof_area = roof_area
self.roof_insulator = roof_insulator
self.wall_area = wall_area
self.wall_insulator = wall_insulator
def calculate_roof_insulation_packages(self):
return self.roof_area / self.roof_insulator.area
def calculate_wall_insulation_packages(self, insulator):
return self.wall_area / self.wall_insulator.area
你会用这样的东西创建房子实例(我正在为 Insulator
class 编造参数,所以不要太注意那部分):
good_roof_insulation = Insulator(4, 5) # nonsense args
cheap_wall_insulation = Insulator(5, 12)
my_house = House(100, good_roof_insulation, 87, cheap_wall_insulation)
首先,我是一个新手。感谢您的耐心等待。
我正在设计一款软件来计算不同房屋的绝缘 materials 和数量。
我有一个 class House()
,它包含 roof_area 和 wall_area 等属性。
我有一个 class Insulator()
,它包含厚度和面积(打包 material 覆盖的区域)等属性
现在我想知道我应该购买多少包的绝缘子才能覆盖整个屋顶区域。
因此,操作将是:
House.roof_area / Insulator.area = insulator_packages_needed_for_roof
问题是我无法执行该操作:
AttributeError: type object 'House' has no attribute 'roof_area'.
当然我可以在一个实例范围内完成它,但我还不想指定一个实例,因为这个操作应该对将来构建的 Class 的任何实例完成.我应该使用继承吗?我的感觉是,Insulator和House是完全不同的东西,不应该继承混用,但我只是初学者
如果您想在不创建实例的情况下使用属性,您应该使用 class 个属性。
class House(object):
roof_area = 10 # Or whatever you see fit.
roof_surface = 20
class Insulator(object):
thickness = 10 # Or whatever you see fit.
surface = 20
这样您就可以通过键入 'House.roof_area' 来访问属性。
虽然,我不明白为什么你不能创建一个实例。它会防止在 class 属性中进行硬编码,并且在您的情况下会容易得多。
此外,您的操作语法无效,但也许您只是展示了伪代码。正确的语法是:
insulator_packages_needed_for_roof = House.roof_area / Insulator.area
在不使用 House
或 Insulator
classes。只有每个实例都有一个实例才有意义。
但是,您可以在创建实例之前编写代码来进行计算。只需将它放在一个将实例作为参数的函数中:
def roof_insulation_packages(house, insulator): # args are instances of House and Insulator
return house.roof_area / insulator.area # do the calculation and return it
将函数作为 classes 之一的方法可能更有意义。我什至建议 Insulator
实例可能是 House
实例属性的一个很好的候选者。那看起来像这样:
class House():
def __init__(self, roof_area, roof_insulator, wall_area, wall_insulator):
self.roof_area = roof_area
self.roof_insulator = roof_insulator
self.wall_area = wall_area
self.wall_insulator = wall_insulator
def calculate_roof_insulation_packages(self):
return self.roof_area / self.roof_insulator.area
def calculate_wall_insulation_packages(self, insulator):
return self.wall_area / self.wall_insulator.area
你会用这样的东西创建房子实例(我正在为 Insulator
class 编造参数,所以不要太注意那部分):
good_roof_insulation = Insulator(4, 5) # nonsense args
cheap_wall_insulation = Insulator(5, 12)
my_house = House(100, good_roof_insulation, 87, cheap_wall_insulation)