TypeError:__ init __()需要4个参数(给定1个)

时间:2015-09-25 21:21:52

标签: python typeerror

我需要帮助,我在Python中有以下带继承的类,我有一个错误:

class Human:

    def __init__(self,name,surname,age):
        self.name = name
        self.surname = surname
        self.age = age

    def getName(self):
        return self.name

    def getSurname(self):
        return self.surname

    def setName(self, name):
        self.name = name

    def setSurname(self, surname):
        self.surname = surname

    def setAge(self, age):
        self.age = age

    def getAge(self):
        return self.age
    pass

from Human import Human

class Student(Human):

    def __init__(self,name,surname,age,file):
        Human().__init__(self,name, surname, file)
        self.file = file

    def getFile(self):
        return self.file

    def setFile(self, file):
        self.file = file

    pass

当我实例化以下错误时

from Student import Student
student1 = Student("Jhon", "Santana", "20", "111000")

错误:

Human().__init__(self, name, surname, age)
TypeError: __init__() takes exactly 4 arguments (1 given)

导致此错误的原因是什么?感谢...

4 个答案:

答案 0 :(得分:3)

Human().__init__(self,name, surname, age)

这不是你如何创建你的类的实例

你应该这样做:

Human.__init__(self,name, surname, age)

没有()。另外,您尝试在Human()

中创建它的实例

答案 1 :(得分:2)

不要实现你的父类

secondRandValue

甚至更好

def __init__(self,name,surname,age,file):
    Human.__init__(self,name, surname, file)

答案 2 :(得分:2)

试试这个:

import { Component, View, bootstrap, OnInit} from 'angular2/angular2';
import 'reflect-metadata';
import 'winjs';

@Component({
    selector: 'my-app'
})
@View({
    template: '<div data-win-control=\'WinJS.UI.Rating\' data-win-options=\'{averageRating: 3.4}\'></div>'
})
class MyAppComponent implements OnInit {
    onInit() {
        WinJS.UI.processAll();
    }
}

当您继承另一个类但想要覆盖某些属性时,需要在超级构造函数中添加要覆盖的父类的属性,以便传递给student类的属性可以直接传递给父类类。

欢迎在以下地址观看我的继承视频: https://www.youtube.com/watch?v=cqRtcmPGcic

答案 3 :(得分:1)

详细here,优先使用super

def __init__(self, name, surname, age, file):
    super(Human, self).__init__(name, surname, age)
    self.file = file