|
Tim 说: 我 25 岁 tizhong 80 爱好 <__main__.people object at 0x7fa7ea1e2860> 。
想问下 为什么 子类继承pepole 类后 luxing 的结果变了呢?
class people:
#定义基本属性
name = ''
age = 0
#定义私有属性,私有属性在类外部无法直接进行访问
__weight = 0
#定义构造方法
def __init__(self,n,a,w,p):
self.name = n
self.age = a
self.__weight = w
self.aihao = p
def pspeak(self):
print("%s 说: 我 %d 岁 tizhong %d 爱好 %s 。" %(self.name,self.age,self.__weight,self.aihao))
p = people('yys',18,180,"wan")
p.pspeak()
#另一个类,多重继承之前的准备
class speaker():
topic = ''
name = ''
def __init__(self,n,t):
self.name = n
self.topic = t
def speak(self):
print("我叫 %s,我是一个演说家,我演讲的主题是 %s"%(self.name,self.topic))
#多重继承
class sample(people,speaker): #继承speaker和student类
def __init__(self,n,a,w,g,t):
#student.__init__(self,n,a,w,g)
speaker.__init__(self,n,t)
people.__init__(self,n,a,w,p)
test = sample("Tim",25,80,"luxing","python")
test.speak() #方法名同,speak就是方法,默认调用的是在括号中排前地父类的方法
test.pspeak()
执行后:
yys 说: 我 18 岁 tizhong 180 爱好 wan 。我叫 Tim,我是一个演说家,我演讲的主题是 pythonTim 说: 我 25 岁 tizhong 80 爱好 <__main__.people object at 0x7fa7ea1e2860> 。
想问下 为什么 子类继承pepole 类后 luxing 的结果变了呢?
|
|