component-example.py

来自「大型多人在线游戏开发,该书光盘上附带的源码」· Python 代码 · 共 22 行

PY
22 行
字号
from twisted.python.components import getAdapter, registerAdapter, Interfaceclass IA(Interface):                    # Define an interface that implements     def a(self):                        # a sample method method, "a".        "A sample method."class A:                                # define an adapter class    __implements__ = IA                 # that implements our IA interface    def __init__(self, original):       # keep track of the object being wrapped        self.original = original    def a(self):                        # define the method required by our        print 'a',                      # interface, and have it print 'a'        self.original.b()               # then call back to the object we're adaptingclass B:                                # the hapless B class doesn't know anything    def b(self):                        # about its adapter, and it defines one        print 'b'                       # method which displays 'b'registerAdapter(A, B, IA)               # register A to adapt B for interface IAadapter = getAdapter(B(), IA, None)     # adapt a new B instance with an A instanceadapter.a()                             # call the method defined by interface IA

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?