【设计开发】 Python学习笔记-super() argument 1 must be type, not classobj

 

 1 #!/usr/bin/python
 2 # -*- coding:UTF-8 -*-
 3 
 4 class Person():
 5   "A Person class"
 6 
 7   def __init__(self,name,age):
 8     self.name =name
 9     self.__age = age
10 
11   def Introduce(self):
12     print "Hi, My name is:%s,I'm %d years-old."%(self.name,self.__age)
13 
14 
15 class Student(Person):
16   "A student class <- Person"
17 
18   def __init__(self,name,age,score):
19     Person.__init__(self,name,age)  #Notice:If don't call person.__init__ then Student class will lose name and age.
20     self.score = score
21   
22   def ReportScore(self):
23     super(Student,self).Introduce()
24     print "My score is:%d"%(self.score)
25 
26   def __str__(self):
27     return "This is a student class"
28 
29 
30 s1=Student('A',20,80)
31 
32 #s1.Introduce()
33 s1.ReportScore()

 

以上代码第23行报错:

➜  Python git:(master) ✗ python oop_syntax.py
Traceback (most recent call last):
  File "oop_syntax.py", line 33, in <module>
    s1.ReportScore()
  File "oop_syntax.py", line 23, in ReportScore
    super(Student,self).Introduce()
TypeError: super() argument 1 must be type, not classobj

 

参考做法:https://blog.csdn.net/andos/article/details/8973368

将Person改为新类,问题解决。

 1 #!/usr/bin/python
 2 # -*- coding:UTF-8 -*-
 3 
 4 class Person(object):
 5   "A Person class"
 6 
 7   def __init__(self,name,age):
 8     self.name =name
 9     self.__age = age
10 
11   def Introduce(self):
12     print "Hi, My name is:%s,I'm %d years-old."%(self.name,self.__age)

 

转载于:https://www.cnblogs.com/digital-wei/p/10295172.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值