# from abc import ABCMeta,abstractmethod # # # class Payment(metaclass=ABCMeta): # 抽象类(接口类): # @abstractmethod # def pay(self): pass # 制定了一个规范 # # @abstractmethod # # def func(self):pass # # # class Alipay(Payment): # def __init__(self,money): # self.money = money # # def pay(self): # print('使用支付宝支付了%s' %self.money) # # # class Jdpay(Payment): # def __init__(self, money): # self.money = money # # def pay(self): # print('使用京东支付了%s' % self.money) # # class Wechatpay(Payment): # # def __init__(self,money): # self.money = money # # def pay(self): # print('使用微信支付了%s' % self.money) # # # def pay(obj): # obj.pay() # w1 = Wechatpay(200) # a1 = Alipay(200) # j1 = Jdpay(100) # pay(a1) # 归一化设计 # pay(j1) # from abc import abstractmethod,ABCMeta # class payment(metaclass=ABCMeta): # @abstractmethod # def pay(cls): pass # from abc import abstractmethod,ABCMeta # class Payment(metaclass=ABCMeta): # @abstractmethod # def pay(self): # pass from abc import abstractmethod,ABCMeta class Payment(metaclass=ABCMeta): @abstractmethod def pay(self): pass class Alipay(Payment): def __init__(self,money): self.money=money def pay(self): print("支付宝支付%s元"%self.money) class Jdpay(Payment): def __init__(self,money): self.money=money def pay(self): print("京东支付%s"%self.money) class Wchat(Payment): def __init__(self,money): self.money=money def pay(self): print("微信支付%s元"%self.money) def pay(obj): obj.pay()#归一化 # a=Alipay(100) # w=Wchat(200) # pay(a) # pay(w) # 广义的封装: 实例化一个对象,给对象空间封装一些属性. # 狭义的封装: 私有制. # 私有成员:私有静态字段,私有方法,私有对象属性 # 私有静态字段 class A: name="alex" __sex=15 def __init__(self,hobby,age): self.hobby=hobby self.__age=age def eat(self): self.__eat() print(self.__age) print(self.__sex) def __eat(self): print(666) class B(A): num=10 __game="lol" def __init__(self,hobby,age,name,ad): super().__init__(hobby,age) self.name=name self.__ad=ad def drink(self): self.__drink() print(self.__game) print(self.__ad) def __drink(self): print(777) b=B("wan",14,"盖伦",10) # print(b.__ad)#对象的属性也只能在类中访问,父类也不行 # b.drink() b.eat() # B.__drink()#方法也只能在类中访问,父类也不行 # print(b.__game)#动态字段只能在类中访问,父类也不行 # print(b.__dict__) class Parent: def __func(self): print('in Parent func') def __init__(self): self.__func() class Son(Parent): def __func(self): print('in Son func') son1 = Son()#实例化一个son的对象,然后对象空间根据指针找到__init__函数,并且执行函数,执行内容为son1._func() # 又因为该函数在Parent类中,他的函数名为_Parent__func,然后对象空间在找对应的函数执行,一直找到父类中的__func(), # 然后执行,输出结果