#three main features of object-oriented: encapsulation, inheritance, polymorphism#Impact of Inheritance: Resource inheritance, Resource usage, resource coverage, resource accumulation#The inheritance of resources, in Python, refers to resources that can use the parent class, rather than the subclass of the child class that also copies a copy of the parent class code into the subclass .classanimate:a= 1#Public Resources_b = 2#Protected Resources __c= 3#a private resource defT1 (self):Print("T1") def_t2 (self):Print("T2") def __t3(self):Print("T3") #built-in methods def __init__(self):Print("Init, Animate")classPerson (Animate):defTest (self):Print(SELF.A)Print(self._b)#print (self.__c) #访问父类私有的资源会有错误self.t1 () self._t2 ( )#self.__t3 () #访问父类私有的资源会有错误P=Person ()Print(P.test ()) Animate.a= 100#set a Class property value, see if the value of a changes, if it is a change, the representative just has the use of animatep.test ()#Use of resources: (Inherited form: Single inheritance, no overlapping multiple inheritance, overlapping multiple inheritance)Print(Person.mro ())#---------------------polymorphic (Python does not have the true meaning of polymorphism, nor does it require polymorphism-------------------------------classAnimal ():defspeaker (self):PassclassDog (Animal):defspeaker (self):Print("Wangwang")classCat (Animal):defspeaker (self):Print("Miaomiao")defTest (obj): Obj.speaker () d=Dog () C=Cat () test (d) test (c)#---------------------Abstract classes and abstract methods--------------------------ImportABCclassAnimal (Object, metaclass=ABC. Abcmeta): @abc. Abstractmethoddefspeaker (self):PassclassDog (Animal):defspeaker (self):Print("Wangwang") PassclassCat (Animal):defspeaker (self):Print("Miaomiao")defTest (obj): Obj.speaker () d= Dog ()
Python object-oriented three main features: encapsulation, inheritance, polymorphism