Python Object-Oriented small example

Source: Internet
Author: User

1 #!/usr/bin/python2 #-*-coding:utf-8-*-3 #added by Kangye python274 5 classPerson :6     "Basic types of people"7     #declaring class Properties8Name =""9Age =0Ten     #Private Properties One     __sex="Mans" A  -     #Construction Method -     def __init__(Self, name, age, sex="Mans"): theSelf.name =name -Self.age = Age -Self.__sex=Sex -  +     defdisplay (self): -Self.__agecount(3) +         Print "This ' man ' name is%s, and ' is '%d, and sex is%s"% (Self.name, self.age, self.__sex) A         return Self at  -     defgetage (self): -         returnSelf.age -  -     defGetsex (self): -         returnSelf.__sex in  -     #Private Methods to     def __agecount(self, i): +Self.age + =I -  the classMan : *     "Men" $ Panax NotoginsengPower = 10 -  the     def __init__(self, Power, name): +Self.power =Power ASelf.name =name the  +     #method Overrides -     defdisplay (self): $         Print "The name is%s, and the age is%d"%(Self.name, Self.age) $         return Self -  -     defgetage (self): the         Print "calling subclasses" -Self.age + = 7Wuyi         returnSelf.age the  - if __name__=="__main__": Wu  -Persona = person ("Kangye", 12) About     PrintPersona.name $  -     #Chained Programming -     PrintPersona.display (). Getage () -     PrintPersona.getsex () A  +PERSONB = Person ("Kangye", 12,"Handsome boy!") the Personb.display () -  $Mana = Man (100,"Kevin!") the     PrintMana.age the     PrintMana.display (). Getage ()

Python Object-Oriented small example

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.