def phone(): """ 直接返回一个新建的手机实例 可以理解为一个空手机 :return: """ return Phone()
def product(phone_name: str) -> Phone.__subclasses__(): """ 根据传入的型号名称,返回对应型号的类 :param phone_name: :return: """ if phone_name == 'iphone': return iPhoneKit elif phone_name == 'samsung': return SamsungS20Kit elif phone_name == 'huawei': return HuaweiMate30Kit else: return None
def package(phone: Phone, os: OS, logo: Logo): """ 静态方法,用于给一个已经生产完的手机添加操作系统和logo :param phone: :param os: :param logo: :return: """ # 为手机添加一份克隆的os,类似cv phone.os = os.clone() # 为手机添加一个克隆的logo贴纸,类似复印 # 偷懒使用了猴子补丁给phone对象添加属性 setattr(phone, 'logo', logo.clone()) print('os and logo had been add to the phone') print(f"it's logo is {phone.logo.info}") print(f"it's os is {phone.os.info}")
def product(phone_name: str) -> Phone.__subclasses__(): """ 根据传入的数据返回对应的手机类 :param phone_name: :return: """ if phone_name == 'iphone': print('input string "iphone" and return iphone class') return iPhoneKit elif phone_name == 'samsung': print('input string "samsung" and return samsung class') return SamsungS20Kit elif phone_name == 'huawei': print('input string "huawei" and return huawei class') return HuaweiMate30Kit else: return None
:return: """ # 为手机添加一份克隆的os,类似cv phone.os = os.clone() # 为手机添加一个克隆的logo贴纸,类似复印 # 偷懒使用了猴子补丁给phone对象添加属性 setattr(phone, 'logo', logo.clone()) print('os and logo had been add to the phone') print(f"it's logo is {phone.logo.info}") print(f"it's os is {phone.os.info}") if __name__ == '__main__': foxconn = Foxconn() # 实例化一部手机 phone1 = Phone() # 实例化一个iOS的系统 os1 = OS('ios') # 实例化一个苹果的图标 logo1 = Logo('apple') # 实例化另一部手机 phone2 = Phone() # 实例化一个安卓系统 os2 = OS('andriod') # 实例化一个三星的图标 logo2 = Logo('samsung') # 在工厂中,给手机1装上os,并贴上图标 foxconn.package(phone1, os1, logo1) # 比较原来的系统与被装上的系统之间的差别 compare(os1, phone1.os) compare(logo1, phone1.logo)
def __init__(self): self._phone = Phone()
def __init__(self): """ 在类进行初始化时,就创建一个具体的手机实例,用于装配 """ self._phone = Phone()
def phone(): return Phone()