类
特性:
封装
继承
# 继承 class People: def __init__(self, name, age): self.name = name self.age = age def eat(self): print("%s is eating..."%self.name) def sleep(self): print("%s is sleeping...."%self.name) class Man(People): # 继承People def __init__(self, name, age, money): People.__init__(self, name, age) # 调用父类的属性 self.money = money # 新增属性 print("%s 一出生就有%s money"%(self.name, self.money)) def piao(self): print("%s is piaoing...."%self.name) def sleep(self): # 给父类添加新方法 People.sleep(self) # 直接调用父类方法 print("man is sleeping.....") class Woman(People): # 继承父类 def get_birth(self): print("%s is born a baby...."%self.name) m1 = Man("HanChun", 22, 2) # 有2块钱 m1.eat() m1.piao() m1.sleep() w1 = Woman("ChenRong", 26) w1.get_birth()
多态