类模式实现:

  1. 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("不支持该接口类型")
  1. 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("不支持该接口类型")
  1. 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("不支持该接口类型")

对象模式实现:

  1. 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()

  1. 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()

  1. 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 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录