35mm接口及25mm接口;USB Type-c接口;lighting接口。用类模式实现耳机接口的转接。给出代码。
抽象类接口:
from abc import ABC, abstractmethod
class HeadphoneInterface(ABC):
@abstractmethod
def connect(self):
pass
3.5mm接口类:
class ThreePointFiveInterface(HeadphoneInterface):
def connect(self):
print("Connect to 3.5mm interface.")
2.5mm接口类:
class TwoPointFiveInterface(HeadphoneInterface):
def connect(self):
print("Connect to 2.5mm interface.")
USB Type-c接口类:
class TypeCInterface(HeadphoneInterface):
def connect(self):
print("Connect to USB Type-C interface.")
lighting接口类:
class LightingInterface(HeadphoneInterface):
def connect(self):
print("Connect to Lighting interface.")
转接器类:
class Adapter:
def __init__(self, interface):
self.interface = interface
def connect(self):
self.interface.connect()
使用示例:
three_point_five = ThreePointFiveInterface()
two_point_five = TwoPointFiveInterface()
type_c = TypeCInterface()
lighting = LightingInterface()
adapter1 = Adapter(three_point_five)
adapter2 = Adapter(two_point_five)
adapter3 = Adapter(type_c)
adapter4 = Adapter(lighting)
adapter1.connect() # Connect to 3.5mm interface.
adapter2.connect() # Connect to 2.5mm interface.
adapter3.connect() # Connect to USB Type-C interface.
adapter4.connect() # Connect to Lighting interface.
``
原文地址: https://www.cveoy.top/t/topic/hjjr 著作权归作者所有。请勿转载和采集!