35mm接口及25mm接口;USB Type—C接口;lighting接口分别用类模式和对象模式实现耳机接口的转换写出对应代码
类模式实现:
- 3.5mm接口及2.5mm接口转换类
class AudioAdapter: def init(self, audio_port): self.audio_port = audio_port
def connect(self):
if self.audio_port == "3.5mm":
print("3.5mm耳机已连接")
elif self.audio_port == "2.5mm":
print("2.5mm耳机已连接")
else:
print("不支持该接口类型")
- USB Type-C接口转换类
class UsbAdapter: def init(self, usb_port): self.usb_port = usb_port
def connect(self):
if self.usb_port == "Type-C":
print("USB Type-C耳机已连接")
else:
print("不支持该接口类型")
- lighting接口转换类
class LightingAdapter: def init(self, lighting_port): self.lighting_port = lighting_port
def connect(self):
if self.lighting_port == "lighting":
print("Lighting耳机已连接")
else:
print("不支持该接口类型")
对象模式实现:
- 3.5mm接口及2.5mm接口转换类
class AudioAdapter: def init(self, audio_port): self.audio_port = audio_port
def connect(self):
if self.audio_port == "3.5mm":
print("3.5mm耳机已连接")
elif self.audio_port == "2.5mm":
print("2.5mm耳机已连接")
else:
print("不支持该接口类型")
audio_adapter = AudioAdapter("3.5mm") audio_adapter.connect()
- USB Type-C接口转换类
class UsbAdapter: def init(self, usb_port): self.usb_port = usb_port
def connect(self):
if self.usb_port == "Type-C":
print("USB Type-C耳机已连接")
else:
print("不支持该接口类型")
usb_adapter = UsbAdapter("Type-C") usb_adapter.connect()
- lighting接口转换类
class LightingAdapter: def init(self, lighting_port): self.lighting_port = lighting_port
def connect(self):
if self.lighting_port == "lighting":
print("Lighting耳机已连接")
else:
print("不支持该接口类型")
lighting_adapter = LightingAdapter("lighting") lighting_adapter.connect(
原文地址: https://www.cveoy.top/t/topic/hjjv 著作权归作者所有。请勿转载和采集!