1 #!/usr/bin/python 2 # -*- coding: UTF-8 -*- 3 # added by kangye python27 4 5 class Person: 6 "人的基本类型" 7 # 申明类属性 8 name = "" 9 age = 0 10 # 私有属性 11 __sex = "man" 12 13 # 构造方法 14 def __init__(self, name, age , sex="man"): 15 self.name = name 16 self.age = age 17 self.__sex = sex 18 19 def display(self): 20 self.__ageCount(3) 21 print "this man name is %s , and age is %d , and sex is %s"%(self.name, self.age, self.__sex) 22 return self 23 24 def getAge(self): 25 return self.age 26 27 def getSex(self): 28 return self.__sex 29 30 # 私有方法 31 def __ageCount(self, i): 32 self.age += i 33 34 class Man(Person): 35 "男人" 36 37 power = 10 38 39 def __init__(self, power, name): 40 self.power = power 41 self.name = name 42 43 # 方法覆盖 44 def display(self): 45 print "the name is %s, the age is %d"%(self.name, self.age) 46 return self 47 48 def getAge(self): 49 print "调用子类" 50 self.age += 7 51 return self.age 52 53 if __name__ == "__main__": 54 55 persona = Person("kangye",12) 56 print persona.name 57 58 # 链式编程 59 print persona.display().getAge() 60 print persona.getSex() 61 62 personb = Person("kangye",12,"handsome boy!") 63 personb.display() 64 65 mana = Man(100, "kevin !") 66 print mana.age 67 print mana.display().getAge()
时间: 2024-10-24 20:30:55