Inheritance of Python classes

Source: Internet
Author: User

#InheritanceclassPerson :defEat (self):Print("Eating ...")        defRun (self):Print("runing ...")#inherited SyntaxclassStudent (person):defStudy (self):Print("study ...") STU1=Student () stu1.run ( )
#rewriteclassPerson :defEat (self):Print("Eating ...")        defRun (self):Print("runing ...")#subclasses overriding parent class methodsclassStudent (person):defRun (self):Print("Quick runing ...")                Print("father Func")                #calling a parent class method in a subclass                #第一种方法: Note that you need to pass the argument self at this timePerson.run (self)#second method: Calling the parent class through the super () methodsuper (). Run ()defStudy (self):Print("study ...") STU1=Student () stu1.run ( )

#the inheritance of private methods or private properties in a classclassPerson :def __init__(self): Self.name="Tom"Self .__age= 14def __gettom(self):Print(self.)__age)classStudent (person):defShow (self):#child class cannot inherit private member property of parent class                #Print ("name is%s, and is%d."% ( Self.name,self.__age))                Print("name is%s"%(self.name))defShowtom (self):#subclasses cannot inherit private methods of parent class                #__gettom ()Stu=Student () stu.showtom ( )

Inheritance of Python classes

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.