又来求助了,大神求解答 python类继承的问题

2024-12-28 22:07:28
推荐回答(3个)
回答1:

#_*_encoding:utf-8_*_

class people:
#定义基本属性
name = ''
age = 0
#定义私有属性,私有属性在类的外部无法直接访问
__weight = 0
#定义构造方法
def __init__(self,a,w):#调用时需传入self相当于this
self.name = 'MHC'
self.age = a
self.weight = w
def speak(self):
print "%s is speaking: I am %d years old."%(self.name,self.age)

p = people(10,30)
p.speak()

class student(people):
grade = 0
def __init__(self,a,w,g):
#调用父类的构造函数
people.__init__(self,a,w)
self.grade = g
#覆写父类speak方法
def speak(self):
print "%s is speaking: I'am %d years,and I'm in grade %s "%(self.nameself.age,self.grade)

s = student(100,60,3,100)
s.speak()

回答2:

你这什么都没有解答什么啊,至少代码贴一下吧

回答3:

google的大神来了,都无法为你解答,