Python implements the Adapter mode instance code, pythonadapter
This article focuses on the implementation of the Adapter mode in python. The specific implementation code is as follows.
The Adapter mode can be implemented in two ways: class mode.
# Understanding # The principle of the power adapter. The incompatible interface class can work # This is the class implementation method # example # If an outlet class output foot is 3 feet, the lamp requires a two-pin socket, and now an Adapter is needed to implement the Adapter socket # Adaptee class socket (object): def Trigle (self ): print 'power supply' # target class tableLamp (object): def needTwo (self): pass # adapter class Adapter (tableLamp, socket): def needTwo (self): self. trigle () # client if _ name __= = '_ main _': lamp = Adapter () lamp. needTwo ()
Run
The second is the object method.
# This is the object implementation method class socket (object): def Trigle (self): print 'power supply over' # target class tableLamp (object): def needTwo (self ): pass # adapter class Adapter (tableLamp): def _ init _ (self, Socket): self. socket = Socket def needTwo (self): self. socket. trigle () # client if _ name __= = '_ main _': plug = socket () lamp = Adapter (plug) lamp. needTwo ()
Run
Summary
The above is all about the python Implementation of the Adapter mode instance code, and I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!