你的问题真的很模棱两可。
您说您希望父类 Fruits 包含 类型为 Orange/Apple 等的对象。但您也说取决于获得的类创造了你想做的事。
*如果条件匹配... . (什么条件??)你没有指定什么条件。根据您提供的内容,我对答案应该是什么做出了解释。
class Fruit(object):
color = None
values = None
nature = None
def __init__(self, color, values, nature):
self.color = color
self.values = values
self.nature = nature
class Orange(Fruit):
color = "Orange"
def __init__(self, values, nature):
super(Orange, self).__init__(self.color, values, nature)
class Apple(Fruit):
color = "Red"
def __init__(self, values, nature):
super(Apple, self).__init__(self.color, values, nature)
# a = Fruit("Green", (3,4), "Organic")
l = []
l.append(Fruit("Green", (3,4), "Organinc"))
l.append(Orange((3,4), "Non Organic"))
l.append(Apple((4,3), "Organic"))
print l
for f in l:
if type(f) is Orange:
print "Found an orange"