component-example.py
上传用户:ghyvgy
上传日期:2009-05-26
资源大小:547k
文件大小:1k
源码类别:

其他游戏

开发平台:

Python

  1. from twisted.python.components import getAdapter, registerAdapter, Interface
  2. class IA(Interface):                    # Define an interface that implements 
  3.     def a(self):                        # a sample method method, "a".
  4.         "A sample method."
  5. class A:                                # define an adapter class
  6.     __implements__ = IA                 # that implements our IA interface
  7.     def __init__(self, original):       # keep track of the object being wrapped
  8.         self.original = original
  9.     def a(self):                        # define the method required by our
  10.         print 'a',                      # interface, and have it print 'a'
  11.         self.original.b()               # then call back to the object we're adapting
  12. class B:                                # the hapless B class doesn't know anything
  13.     def b(self):                        # about its adapter, and it defines one
  14.         print 'b'                       # method which displays 'b'
  15. registerAdapter(A, B, IA)               # register A to adapt B for interface IA
  16. adapter = getAdapter(B(), IA, None)     # adapt a new B instance with an A instance
  17. adapter.a()                             # call the method defined by interface IA