#父类Person class Person(): def __init__(self,name,age,hair): self.name = name self.age = age self.hair = hair def talk(self): print("Person is talking----") #子类 Chinese class Chinese(Person): #先继承 Person,再重构自身类 def __init__(self,name,age,language,hair): # name age hair 都是继承的父类 Person Person.__init__(self,name,age,hair) #继承父类的构造方法 self.language = language # Chinese 类中的 language, name、age、hair 已继承父类Person def walk(self): print("Person is walking----") c = Chinese('phone',21,'French','yello') print (c.name) print (c.age) print (c.language) print(c.hair) 运行结果: phone 21 French yello
#父类Person class Person(): def __init__(self,name,age,hair): self.name = name self.age = age self.hair = hair def talk(self): print("Person is talking----") #子类 Chinese class Chinese(Person): #先继承 Person,再重构自身类 def __init__(self,name,age,language,hair): # name age hair 都是继承的父类 Person Person.__init__(self,name,age,hair) #继承父类的构造方法 self.language = language # Chinese 类中的 language, name、age、hair 已继承父类Person def walk(self): print("child is walking----") # talk 方法,在子类当中重构该方法 def talk(self): print("child is talking-----") c = Chinese('phone',21,'French','yello') print(c.talk()) 打印结果: child is talking-----