在刚学习python的时候,学习类的继承的时候,自己凭记忆手工敲了下代码,运行的时候报了如下错误:
C:\Python27\python.exe C:/Users/yangfeng/PycharmProjects/untitled/class_hbk/test.py
Traceback (most recent call last):
File "C:/Users/yangfeng/PycharmProjects/untitled/class_hbk/test.py", line 21, in <module>
hbk = Man()
File "C:/Users/yangfeng/PycharmProjects/untitled/class_hbk/test.py", line 12, in __init__
super(Man, self).__init__(self.age,self.weight,self.name)
TypeError: super() argument 1 must be type, not classobj
Process finished with exit code 1
编写的代码如下:
#!/usr/bin/python
# -*- coding:UTF-8 -*-
class Person:
def __init__(self,age,weight,name):
self.age=age
self.weight=weight
self.name=name
def introduce(self):
print 'My name is %s,age=%s,weight=%s'%(self.name,self.age,self.weight)
class Man(Person):
def __init__(self,age,weight,name,sex):
super(Man, self).__init__(age,weight,name)
self.sex=sex
def introduce(self):
print 'I am Man,name=%s,age=%s,weight=%s,sex=%s'%(self.name,self.age,self.weight,self.sex)
class Woman(Person):
def introduce(self):
print 'I am Woman.name=%s,age=%s,weight=%s'%(self.name,self.age,self.weight)
if __name__=='__main__':
hbk = Man("29","170","黄宝康","男")
zll = Woman("28","100","张露露")
p = Person("999","999","person")
hbk.introduce()
zll.introduce()
p.introduce()
查找资料之后发现,python中super只能应用于新类,而不能应用于经典类
所谓新类就是所有类都必须要有继承的类,如果什么都不想继承,就继承到object类。
所谓经典类就是什么都不用继承的类。
解决方法,改成Person(object)即可。