需要Python类和对象方面的帮助吗
问题陈述: 将一些值视为: 水果--> 苹果(红色,(3,2),有机), 橙色(橙色,(5,2),非有机) 等等 我想将父类定义为水果,然后在此父类中定义具有多个值的对象 然后,如果条件匹配,并且创建了类Oranges,我想运行一个只针对类Oranges的特定函数 我不熟悉Python中如此复杂的编程需要Python类和对象方面的帮助吗,python,Python,问题陈述: 将一些值视为: 水果--> 苹果(红色,(3,2),有机), 橙色(橙色,(5,2),非有机) 等等 我想将父类定义为水果,然后在此父类中定义具有多个值的对象 然后,如果条件匹配,并且创建了类Oranges,我想运行一个只针对类Oranges的特定函数 我不熟悉Python中如此复杂的编程 开放的建议,以及 看起来您需要使用多重继承 class Fruits(object): def __init__(self, fruit): print fruit + "
开放的建议,以及 看起来您需要使用多重继承
class Fruits(object):
def __init__(self, fruit):
print fruit + "is a fruit"
class Organic(Fruits):
def __init__(self, fruit):
print fruit + "is organic"
super(Organic, self).__init__(fruit)
class Colored(Organic):
def __init__(self, fruit, color):
print fruit + "is " + color
super(Colored, self).__init__(fruit)
class Apple(Colored, Organic):
def __init__(self):
super(Apple, self).__init__("apple", "red")
apple = Apple()
你的问题真是模棱两可 你说你想让父类
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"
元组是什么意思?我猜您也想要Python2.x?只是一个示例,想展示值的层次结构。。。Python2.x您能举个例子说明您到目前为止所做的尝试以及遇到的问题吗?