<p>我正在尝试创建一个类,允许用户创建一个自定义按钮对象,该对象包含按钮的外观属性,以及一个函数,我希望在调用按钮的executeFunction()命令时能够运行该函数。你知道吗</p>
<pre><code>def foo():
print "bar"
class Button(object):
def __init__(self, name, color, function):
self.name = name
self.color = color
self.function = function
# I want to be able to run the function by calling this method
def executeFunction(self):
self.function()
newButton = Button("Example", red, foo())
newButton.executeFunction()
</code></pre>
<p>这是正确的方法,还是有具体的方法来执行这种操作?你知道吗</p>