|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
class Parent(): #定义父类
parentAttr=100
def __init_(self):
print('调用父类的构造函数')
def parentMethod(self):
print('调用父类的方法')
def setAttr(self,attr):
Parent.parentAttr=attr
def getAttr(self):
print('父类属性:',Parent.parentAttr)
def Child(Parent): #定义子类
def __init__(self):
print('调用子类的构造函数')
def childMethod(self):
print('调用子类的方法')
c=Child(Parent) #实例化子类
c.childMethod() #调用子类的方法
c.parentMethod() #调用父类的方法
c.setAttr(200) #再次调用父类的方法-设置属性值
c.getAttr() #再次调用父类的方法-获取属性值
麻烦前辈们帮我看看这个哪里出了问题,不知道怎么改
我发现有个地方你原先代码写错了,我没有纠正,所以出现的问题
你把Child 类定义成了函数
正确代码应该是如下:
- class Parent(): #定义父类
- parentAttr=100
- def __init_(self):
- print('调用父类的构造函数')
- def parentMethod(self):
- print('调用父类的方法')
- def setAttr(self,attr):
- self.parentAttr=attr #这里是self.parentAttr,指向自身属性parentAttr
- def getAttr(self):
- print('父类属性:',self.parentAttr) #这里也是self.parentAttr,传入自身属性parentAttr
- class Child(Parent): #定义子类,你这里写错成了def,我改成了class
- def __init__(self):
- print('调用子类的构造函数')
- def childMethod(self):
- print('调用子类的方法')
-
复制代码
如果解决了你的问题,请帮忙设定一下最佳回答,谢谢
|
|