我目前正在学习继承和多态,这段代码应该测试这些概念。当我尝试实例化子类对象时,init构造函数不会识别该方法。这是代码。
class Employee:
# constructor
def __init__(self, name, empnum):
self.__empName = name
self.__empNum = empnum
#setName(name)
#setNumber(empnum)
# setters
def setName(self, name):
self.__empName = name
def setNumber(self, number):
if len(str(number)) == 5:
self.__empNum = number
else:
print('Too many or too little numbers entered. Please try again')
setNumber()
# getters
def getName(self):
return self.__empName
def getNumber(self):
return self.__empNum
class ProductionWorker(Employee):
# Overwritten constructor
def __init__(self, name, empnum, shiftnum, rate):
self.setShiftNum(shiftnum)
self.setPayRate(rate)
# Calls Superclass Method
Employee.__innit__(self, name, empnum)
# Setters
def setShiftNum(self, num):
if num == 1 | 2:
self.__shiftNum = num
else:
print('Shift number needs to be a 1 or 2. Please try again')
setShiftNum(self, num)
def setPayRate(self, rate):
self.__payRate = '$' + str(format(rate, ',.2f'))
# Getters
def getShiftNum(self):
return self.__shiftNum
def getPayRate(self):
return self.__payRate
def main():
gruntling = Employee('Farlo', 53400)
print('Your grunts name is' + gruntling.getName())
print('this program does things. We swears.')
grunty = ProductionWorker('Farlo', 45300, 2, 4.25)
print('Lil grunty\'s name is ' + grunty.getName())
main()
我得到的错误是:
Traceback (most recent call last):
File "C:/Users/sessh/Dropbox/Spring >2016/Python/Assignments/S13/company_employee.py", line 69, in <module>
main()
File "C:/Users/sessh/Dropbox/Spring >2016/Python/Assignments/S13/company_employee.py", line 66, in main
grunty = ProductionWorker('Farlo', 45300, 2, 4.25)
File "C:/Users/sessh/Dropbox/Spring >2016/Python/Assignments/S13/company_employee.py", line 38, in __init__
self.setShiftNum(shiftnum)
File "C:/Users/sessh/Dropbox/Spring >2016/Python/Assignments/S13/company_employee.py", line 49, in setShiftNum
setShiftNum(self, num)
NameError: name 'setShiftNum' is not defined
编
我浏览了周围,我能找到的最接近的例子是 Calling a class function inside of __init__
我能找到的最好的解决方法是在init中的函数中添加 self ,但后来我得到了一些关于参数太多的奇怪错误。我说的很疯狂!
感谢您的时间。
EDIT。好吧,我读了一些评论并改变了函数的逻辑,给了我一些问题。此外,我没有意识到错误完全是由于编译器没有看到该功能,因为它很糟糕。这是一个有趣的知识:
# Overwritten constructor
def __init__(self, name, empnum, shiftnum, rate):
#self.__shiftNum = shiftnum
self.__payRate = rate
self.setShiftNum(shiftnum)
# Calls Superclass Method
Employee.__init__(self, name, empnum)
# Setters
def setShiftNum(self, num):
shiftnum = num
if shiftnum == 1 or shiftnum == 2:
self.__shiftNum = shiftnum
else:
while True:
shiftnum = input('Shift Number MUST BE a 1 or a 2. Enter it now: ')
if shiftnum == 1 or shiftnum == 2:
self.__shiftNum = numshiftnum
break
else:
continue
正如您之前使用递归函数所看到的那样。我喜欢无限循环。所以我创建了另一个,没有任何意图我向你保证。如果1或2传递给它,这个代码实际上是有效的,但是如果它不是你被困在被动攻击性程序的永无止境的循环中,要求你的服从1和2 2&#39;
在这一点上,我似乎只是坚持这种情况的逻辑,所以我认为我接近解决方案。我觉得逻辑的算法复杂度非常差,但是,此时,我只是想让它起作用。
答案 0 :(得分:0)
def setShiftNum(self, num):
if num == 1 or num == 2:
self.__shiftNum = num
else:
print('Shift number needs to be a 1 or 2. Please try again')
self.setShiftNum(num)
请注意您的逻辑使其陷入无限循环
为了防止这种情况,可以考虑将递归块替换为self.setShiftNum(num = 1)
或更改逻辑。