Python object-oriented three main features: encapsulation, inheritance, polymorphism

Source: Internet
Author: User

#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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.