写法一:
# Author:Sooele class People: #父类 def __init__(self,name,age): self.name = name def eat(self): print("%s is eating.." %self.name) def sleep(self): print("%s is sleeping.." %self.name) def talk(self): print("%s is talking.." %self.name) class Man(People): #子类 def __init__(self,name,age,money): ######重构函数 People.__init__(self,name,age) ######重构函数 self.money = money ######重构函数 print("%s Sooele出生就有 %s money" % (self.name,self.money)) ######重构函数 def piao(self): print("%s in piaoing..... 20s......done" % self.name) def sleep(self): People.sleep(self) #调用父类方法 print("man si sleeping") #调用子类方法 class Woman(People): #子类2 def get_birth(self): print("%s in born a baby..." % self.name) m1 = Man("Sooele",22,1122) ##定义子类1 #1122重构函数值 m1.eat() # m1.piao() # m1.sleep() # m1.money m2 = Woman("Cooi",22) #定义子类2 m2.get_birth() ###子类2不能调用子类1的方法 ##子类1、子类2都可以调用父类方法 0
写法2:
# Author:Sooele class People: #父类 def __init__(self,name,age): self.name = name def eat(self): print("%s is eating.." %self.name) def sleep(self): print("%s is sleeping.." %self.name) def talk(self): print("%s is talking.." %self.name) class Man(People): #子类 def __init__(self,name,age,money): ######重构函数 #People.__init__(self,name,age) ######重构函数 super(Man,self).__init__(name,age) self.money = money ######重构函数 print("%s Sooele出生就有 %s money" % (self.name,self.money)) ######重构函数 def piao(self): print("%s in piaoing..... 20s......done" % self.name) def sleep(self): People.sleep(self) #调用父类方法 print("man si sleeping") #调用子类方法 class Woman(People): #子类2 def get_birth(self): print("%s in born a baby..." % self.name) m1 = Man("Sooele",22,1122) ##定义子类1 #1122重构函数值 m1.eat() # m1.piao() # m1.sleep() # m1.money m2 = Woman("Cooi",22) #定义子类2 m2.get_birth() ###子类2不能调用子类1的方法 ##子类1、子类2都可以调用父类方法