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