I'm learning about inheritance and polymorphism at the moment and this code is supposed to test those concepts. When I attempt to instantiate the subclass object the init constructor doesn't recognize the method. Here's the code.
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()
The error I get is:
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
ed
I browsed around and the closest example I could find was Calling a class function inside of __init__
The best fix I could find was to add self to the function within init but then I got some weird error about there being too many arguments. It's madness I say!
Thank you for your time.
EDIT. Alright, I read some of the comments and altered the logic of the function giving me issues. Also, I didn't realize that the error was completely due to the compiler not seeing the function because it was bad. That's an interesting bit of knowledge:
# 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
So as you saw with the recursive function previously. I love infinite loops. So I created another one, without any sort of intent I assure you. This code actually works if a 1 or 2 is passed to it but if it's not you'll get trapped in a never ending cycle of a passive aggressive program demanding your subservience to typing in 1's and 2's
At this point it seems I'm just stuck on the logic of the situation, so I think I'm close to the solution. I have a feeling the logic has pretty poor Algorithm Complexity but, at this point, I just want it to work.