鱼C论坛

 找回密码
 立即注册
查看: 1272|回复: 5

[已解决]请教关于 ‘’父类和子类‘ 的一个问题

[复制链接]
发表于 2018-6-3 05:34:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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() #再次调用父类的方法-获取属性值

麻烦前辈们帮我看看这个哪里出了问题,不知道怎么改
最佳答案
2018-6-4 13:31:10
stand0328 发表于 2018-6-4 10:24
谢谢回答。不过我用你的代码运行了下,也出错了
>>> c=Child(Parent)
>>> c.childMethod()

我发现有个地方你原先代码写错了,我没有纠正,所以出现的问题
你把Child 类定义成了函数
正确代码应该是如下:

  1. class Parent():  #定义父类
  2.     parentAttr=100
  3.     def __init_(self):
  4.         print('调用父类的构造函数')

  5.     def parentMethod(self):
  6.         print('调用父类的方法')

  7.     def setAttr(self,attr):
  8.         self.parentAttr=attr  #这里是self.parentAttr,指向自身属性parentAttr

  9.     def getAttr(self):
  10.         print('父类属性:',self.parentAttr)   #这里也是self.parentAttr,传入自身属性parentAttr

  11. class Child(Parent): #定义子类,你这里写错成了def,我改成了class
  12.     def __init__(self):
  13.         print('调用子类的构造函数')

  14.     def childMethod(self):
  15.         print('调用子类的方法')
  16.                

复制代码


如果解决了你的问题,请帮忙设定一下最佳回答,谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-6-3 21:22:41 | 显示全部楼层
你没有很好的理解self的含义,对于python这种面向对象的语言来说,你不能使用类名来在类内调用自身属性或方法。
下面将你的代码修正了一下,这里面的含义还是需要你自己取领会的


  1. class Parent():  #定义父类
  2.     parentAttr=100
  3.     def __init_(self):
  4.         print('调用父类的构造函数')

  5.     def parentMethod(self):
  6.         print('调用父类的方法')

  7.     def setAttr(self,attr):
  8.         self.parentAttr=attr  #这里是self.parentAttr,指向自身属性parentAttr

  9.     def getAttr(self):
  10.         print('父类属性:',self.parentAttr)   #这里也是self.parentAttr,传入自身属性parentAttr

  11. def Child(Parent): #定义子类
  12.     def __init__(self):
  13.         print('调用子类的构造函数')

  14.     def childMethod(self):
  15.         print('调用子类的方法')
  16.                

复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-6-4 10:24:21 | 显示全部楼层
simplerjiang 发表于 2018-6-3 21:22
你没有很好的理解self的含义,对于python这种面向对象的语言来说,你不能使用类名来在类内调用自身属性或方 ...

谢谢回答。不过我用你的代码运行了下,也出错了
>>> c=Child(Parent)
>>> c.childMethod()
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    c.childMethod()
AttributeError: 'NoneType' object has no attribute 'childMethod'
请问这是什么原因
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-4 13:27:47 | 显示全部楼层
stand0328 发表于 2018-6-4 10:24
谢谢回答。不过我用你的代码运行了下,也出错了
>>> c=Child(Parent)
>>> c.childMethod()

这里并不是我的代码出错了,是你的输入出错了。
  1. >>> c  = Child() #这个就已经完成输入了,不需要再加上Parent
  2. >>> c.childMethod()  
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-4 13:31:10 | 显示全部楼层    本楼为最佳答案   
stand0328 发表于 2018-6-4 10:24
谢谢回答。不过我用你的代码运行了下,也出错了
>>> c=Child(Parent)
>>> c.childMethod()

我发现有个地方你原先代码写错了,我没有纠正,所以出现的问题
你把Child 类定义成了函数
正确代码应该是如下:

  1. class Parent():  #定义父类
  2.     parentAttr=100
  3.     def __init_(self):
  4.         print('调用父类的构造函数')

  5.     def parentMethod(self):
  6.         print('调用父类的方法')

  7.     def setAttr(self,attr):
  8.         self.parentAttr=attr  #这里是self.parentAttr,指向自身属性parentAttr

  9.     def getAttr(self):
  10.         print('父类属性:',self.parentAttr)   #这里也是self.parentAttr,传入自身属性parentAttr

  11. class Child(Parent): #定义子类,你这里写错成了def,我改成了class
  12.     def __init__(self):
  13.         print('调用子类的构造函数')

  14.     def childMethod(self):
  15.         print('调用子类的方法')
  16.                

复制代码


如果解决了你的问题,请帮忙设定一下最佳回答,谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-6-4 21:02:11 | 显示全部楼层
simplerjiang 发表于 2018-6-4 13:31
我发现有个地方你原先代码写错了,我没有纠正,所以出现的问题
你把Child 类定义成了函数
正确代码应该 ...

啊。还真的是,class错写成def了。非常感谢前辈的帮忙
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-30 23:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表