我正在尝试实现工厂设计模式,目前为止已经做到了这一步。
import abc
class Button(object):
__metaclass__ = abc.ABCMeta
html = ""
def get_html(self, html):
return self.html
class ButtonFactory():
def create_button(self, type):
baseclass = Button()
targetclass = type.baseclass.capitalize()
return targetclass
button_obj = ButtonFactory()
button = ['image', 'input', 'flash']
for b in button:
print button_obj.create_button(b).get_html()
输出应该是所有按钮类型的HTML。
我遇到了这样的错误。
AttributeError: 'str' object has no attribute 'baseclass'
我正在尝试实现一个类,该类具有不同的变体,例如ImageButton、InputButton和FlashButton。根据位置的不同,可能需要为按钮创建不同的 html。
'image'
)作为参数传递给create_button()
函数。也许你想要传递image
(一个实际类的引用,该类具有baseclass
属性)。 - chepnerget_html
,但它需要一个参数(除了self
)。从你的代码中不清楚你想要做什么。 - Elisha