Oop 教授';Python作业的答案将不会运行:TypeError:uuu init_uuu()缺少1个必需的位置参数:';性别';
我肯定以前有人问过这个问题,但我还不了解其他问题如何回答我的问题。有人能解释一下这个代码的问题吗?我看不到:Oop 教授';Python作业的答案将不会运行:TypeError:uuu init_uuu()缺少1个必需的位置参数:';性别';,oop,python-3.6,Oop,Python 3.6,我肯定以前有人问过这个问题,但我还不了解其他问题如何回答我的问题。有人能解释一下这个代码的问题吗?我看不到: class Person: name = '' age = 0 gender = '' def __init__(self, name, age, gender): self.name = name self.age = age self.gender = gender class Employee(P
class Person:
name = ''
age = 0
gender = ''
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
class Employee(Person):
title = ''
salary = 0
def __init__(self, name, age, gender, title, salary):
Person.__init__(name, age, gender)
self.title = title
self.salary = salary
George = Employee("George", 30, "Male", "Manager", 50000)
print("George's info is: Name is %s, Age is %d, Gender is %s, Title is %s, and Salary is %d" % (
George.name, George.age, George.gender, George.title, George.salary))
PyCharm中返回的错误为:
Traceback (most recent call last):
File "/Users/Sam/PycharmProjects/HW7/HW7-q2_Profs-Solution.py", line 22, in <module>
George = Employee("George", 30, "Male", "Manager", 50000)
File "/Users/DrewAndMon/PycharmProjects/HW7/HW7-q2_Profs-Solution.py", line 17, in __init__
Person.__init__(name, age, gender)
TypeError: __init__() missing 1 required positional argument: 'gender'
Process finished with exit code 1
回溯(最近一次呼叫最后一次):
文件“/Users/Sam/PycharmProjects/HW7/HW7-q2_Profs-Solution.py”,第22行,在
乔治=员工(“乔治”,30岁,“男性”,“经理”,50000)
文件“/Users/DrewAndMon/PycharmProjects/HW7/HW7-q2_Profs-Solution.py”,第17行,在__
个人(姓名、年龄、性别)
TypeError:\uuuu init\uuuuu()缺少1个必需的位置参数:“性别”
进程已完成,退出代码为1
需要更改的是对人的呼叫。
在类员工中,定义为def\uu init\uuuu(姓名、年龄、性别):
您的正确代码是-
class Person:
name = ''
age = 0
gender = ''
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
class Employee(Person):
title = ''
salary = 0
def __init__(self, name, age, gender, title, salary):
Person.__init__(self, name, age, gender)
self.title = title
self.salary = salary
George = Employee("George", 30, "Male", "Manager", 50000)
print("George's info is: Name is %s, Age is %d, Gender is %s, Title is %s, and Salary is %d" % (
George.name, George.age, George.gender, George.title, George.salary))
这将为您提供您的输出-
George's info is: Name is George, Age is 30, Gender is Male, Title is Manager, and Salary is 50000
需要更改的是对人员的调用。
在classEmployee
中,对人员的调用是def\uu init\uuu(自我、姓名、年龄、性别):
您的正确代码是-
class Person:
name = ''
age = 0
gender = ''
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
class Employee(Person):
title = ''
salary = 0
def __init__(self, name, age, gender, title, salary):
Person.__init__(self, name, age, gender)
self.title = title
self.salary = salary
George = Employee("George", 30, "Male", "Manager", 50000)
print("George's info is: Name is %s, Age is %d, Gender is %s, Title is %s, and Salary is %d" % (
George.name, George.age, George.gender, George.title, George.salary))
这将为您提供您的输出-
George's info is: Name is George, Age is 30, Gender is Male, Title is Manager, and Salary is 50000
更改此行:
Person.\uuuuu init\uuuuuuuuuu(姓名、年龄、性别)
致:
Person.\uuuuu init\uuuuuuuuuuuuuuuuuuuu(自我、姓名、年龄、性别)
说明:Person.\uuuu init\uuuu
方法包含四个参数,其中第一个参数是正在初始化的对象的self
参数。通常,self
参数的特殊之处在于,它自动获取调用该方法的对象的值。但是,由于Person
是一个类,而不是一个对象,因此在这种情况下,没有对对象调用该方法,因此self
参数必须作为显式参数传递
更正常的做法是将其写成:
super()
这看起来有点奇怪,但它避免了不必要地再次写入父类的名称,并且当一个类有两个超类时,它可以正常工作。更改此行:
Person.\uuuuu init\uuuuuuuuuu(姓名、年龄、性别)
致:
Person.\uuuuu init\uuuuuuuuuuuuuuuuuuuu(自我、姓名、年龄、性别)
说明:Person.\uuuu init\uuuu
方法包含四个参数,其中第一个参数是正在初始化的对象的self
参数。通常,self
参数的特殊之处在于,它自动获取调用该方法的对象的值。但是,由于Person
是一个类,而不是一个对象,因此在这种情况下,没有对对象调用该方法,因此self
参数必须作为显式参数传递
更正常的做法是将其写成:
super()
这看起来有点奇怪,但它避免了不必要地再次写入父类的名称,并且当一个类有两个超类时,它可以正常工作。对Person.\uuu init\uuuuu
的调用应该是Person.\uu init\uuuuu(self)
。或者,最好使用super()。你能链接一个例子吗?调用Person.\uuuu init\uuuu
应该是Person.\uuuu init\uuuu(self)
。或者,最好使用super()。你能链接一个例子吗?我看不到对原始代码的更改。。。??到底是什么?另外,当我将其复制/粘贴到PyCharm中时,它会给出与以前完全相同的错误。我看不到对原始代码的更改。。。??到底是什么?此外,当我将其复制/粘贴到PyCharm中时,它会给出与以前完全相同的错误。