我设置了以下两个类:
class Player:
POINTS_PER_PASSING_YARD = 0.04
POINTS_PER_PASSING_TOUCHDOWN = 4
POINTS_PER_INTERCEPTION = -1
POINTS_PER_RUSHING_YARD = 0.1
POINTS_PER_RUSHING_TOUCHDOWN = 6
POINTS_PER_RUSHING_FUMBLE = -2
POINTS_PER_RECEPTION_YARD = 0.1
POINTS_PER_RECEPTION_TOUCHDOWN = 6
def __init__(self, name, team, rushingYards, rushingTouchdowns, rushingFumbles):
self.name = name
self.team = team
self.rushingYards = rushingYards
self.rushingTouchdowns = rushingTouchdowns
self.rushingFumbles= rushingFumbles
def calculatePoints(self):
return self.rushingYards * POINTS_PER_RUSHING_YARD + self.rushingTouchdowns * POINTS_PER_RUSHING_TOUCHDOWN + self.rushingFumbles * POINTS_PER_RUSHING_FUMBLE
def toString(self):
return "name: " + self.name + " team: " + self.team + " passing yards: " + self.passingYards + " rushing yards: " + self.rushingYards + " touchdowns: " + self.touchdowns + " interceptions: " + self.interceptions
然后我有一个继承自Player的QB类:
来自玩家导入*
class QB(Player):
def __init__(self, name, team, rushingYards, rushingTouchdowns, rushingFumbles, passingYards, passingTouchdowns, interceptions, position="QB"):
super().__init__(self, name, team, rushingYards, rushingTouchdowns, rushingFumbles)
self.passingYards = passingYards
self.passingTouchdowns = passingTouchdowns
self.interceptions = interceptions
def toString(self):
return "position: " + self.position + super().toString()
然后在我的主要课程中,我只是这样做:
myQB = QB("Brees", "Saints", 0, 0, 0, 4952, 33, 17)
print(myQB)
我收到以下错误:
Traceback (most recent call last):
File "main.py", line 35, in <module>
main()
File "main.py", line 32, in main
myQB = QB("Brees", "Saints", 0, 0, 0, 4952, 33, 17)
File "/Users/benjaminclayman/Desktop/Aurora_Fantasy_Football/QB.py", line 5, in __init__
Player.__init__(self, name, team, rushingYards, rushingTouchdowns, rushingFumbles)
TypeError: object.__init__() takes no parameters
但是我不知道为什么,因为我写的所有init方法做都带参数......
知道出了什么问题吗?
谢谢, bclayman
答案 0 :(得分:3)
如果你在第一个脚本(Player
类)中的缩进是正确的,那就是问题。
根据您的缩进,__init__()
方法和其他方法不属于课程Player
,因此Player
使用object
课程&#39; __init__()
,它不接受任何参数(self
除外),但我们不需要明确传递它。
您可能希望修复缩进,以便所有实例方法(您打算在Player
类中)都进入Player
类。
示例 -
class Player:
POINTS_PER_PASSING_YARD = 0.04
POINTS_PER_PASSING_TOUCHDOWN = 4
POINTS_PER_INTERCEPTION = -1
POINTS_PER_RUSHING_YARD = 0.1
POINTS_PER_RUSHING_TOUCHDOWN = 6
POINTS_PER_RUSHING_FUMBLE = -2
POINTS_PER_RECEPTION_YARD = 0.1
POINTS_PER_RECEPTION_TOUCHDOWN = 6
def __init__(self, name, team, rushingYards, rushingTouchdowns, rushingFumbles):
self.name = name
self.team = team
self.rushingYards = rushingYards
self.rushingTouchdowns = rushingTouchdowns
self.rushingFumbles= rushingFumbles
您需要为Player
类中的所有方法执行此操作。
还有一个问题,您不应将self
传递给使用__init__()
调用的te super()
方法。
示例 -
class QB(Player):
def __init__(self, name, team, rushingYards, rushingTouchdowns, rushingFumbles, passingYards, passingTouchdowns, interceptions, position="QB"):
super().__init__(name, team, rushingYards, rushingTouchdowns, rushingFumbles)
答案 1 :(得分:0)
很少有问题,首先你需要修复缩进。
第二,你应该改变
this class Player:
要
class Player(object):
另一个问题是,当您调用静态成员时,这是处理它们的方法:
Player.POINTS_PER_RUSHING_YARD # name of the class and then the static member.
这就是你的超级应该是这样的:
super(QB, self).__init__(name, team, rushingYards, rushingTouchdowns, rushingFumbles)
这是代码:
class Player(object):
POINTS_PER_PASSING_YARD = 0.04
POINTS_PER_PASSING_TOUCHDOWN = 4
POINTS_PER_INTERCEPTION = -1
POINTS_PER_RUSHING_YARD = 0.1
POINTS_PER_RUSHING_TOUCHDOWN = 6
POINTS_PER_RUSHING_FUMBLE = -2
POINTS_PER_RECEPTION_YARD = 0.1
POINTS_PER_RECEPTION_TOUCHDOWN = 6
def __init__(self, name, team, rushingYards, rushingTouchdowns, rushingFumbles):
self.name = name
self.team = team
self.rushingYards = rushingYards
self.rushingTouchdowns = rushingTouchdowns
self.rushingFumbles= rushingFumbles
def calculatePoints(self):
return self.rushingYards * Player.POINTS_PER_RUSHING_YARD + self.rushingTouchdowns * Player.POINTS_PER_RUSHING_TOUCHDOWN + self.rushingFumbles * Player.POINTS_PER_RUSHING_FUMBLE
def toString(self):
return "name: " + self.name + " team: " + self.team + " passing yards: " + self.passingYards + " rushing yards: " + self.rushingYards + " touchdowns: " + self.touchdowns + " interceptions: " + self.interceptions
class QB(Player):
def __init__(self, name, team, rushingYards, rushingTouchdowns, rushingFumbles, passingYards, passingTouchdowns, interceptions, position="QB"):
super(QB, self).__init__(name, team, rushingYards, rushingTouchdowns, rushingFumbles)
self.passingYards = passingYards
self.passingTouchdowns = passingTouchdowns
self.interceptions = interceptions
def toString(self):
return "position: " + self.position + super(Player).toString()
myQB = QB("Brees", "Saints", 0, 0, 0, 4952, 33, 17)
print(myQB)
答案 2 :(得分:0)
在Python中,缩进起主要作用。
问题清单:
1)__init__()
应该在Player类中。否则您的初始化将无效。
2)其他函数def calculatePoints(self
)也应该在class Player
范围内。目前在您的代码中,它是在类外定义的。
3)def toString(self)
也应在class Player
范围内。
3.1) Also the concatenation of string and int is not permitted.
3.2) The variable `self.touchdowns` is not initialized in the def toString of class Player.
3.3) The alternate way to print the class with significant information
about class can be written in this manner.
class Player:
POINTS_PER_PASSING_YARD = 0.04
POINTS_PER_PASSING_TOUCHDOWN = 4
POINTS_PER_INTERCEPTION = -1
POINTS_PER_RUSHING_YARD = 0.1
POINTS_PER_RUSHING_TOUCHDOWN = 6
POINTS_PER_RUSHING_FUMBLE = -2
POINTS_PER_RECEPTION_YARD = 0.1
POINTS_PER_RECEPTION_TOUCHDOWN = 6
def __init__(self, name, team, rushingYards, rushingTouchdowns, rushingFumbles):
self.name = name
self.team = team
self.rushingYards = rushingYards
self.rushingTouchdowns = rushingTouchdowns
self.rushingFumbles= rushingFumbles
def calculatePoints(self):
return self.rushingYards * POINTS_PER_RUSHING_YARD + self.rushingTouchdowns * POINTS_PER_RUSHING_TOUCHDOWN + self.rushingFumbles * POINTS_PER_RUSHING_FUMBLE
def __str__(self):
return "name: %s team: %s passing yards: %r rushing yards: %r rushingTouchdowns: %r interceptions: %r" % (self.name, self.team, self.passingYards, self.rushingYards, self.rushingTouchdowns ,self.interceptions)
4)self.position = position
需要在class QB
初始化,如下所示:
class QB(Player):
def __init__(self, name, team, rushingYards, rushingTouchdowns, rushingFumbles, passingYards, passingTouchdowns, interceptions, position="QB"):
super().__init__(name, team, rushingYards, rushingTouchdowns, rushingFumbles)
self.passingYards = passingYards
self.passingTouchdowns = passingTouchdowns
self.interceptions = interceptions
self.position = position
def __str__(self):
return "position: " + self.position + super().__str__()