Error accessing parent class method [closed]

0
  

Traceback (most recent call last): File   "C: /udemypy/Basic/classes/heranca/ex2/funcionario.py", line 20, in          print (gF.getBonification ()) File "C: /udemypy/Basic/classes/heranca/ex2/funcionario.py", line 16, in   getBonification       return super () getBonification + 1000 AttributeError: 'super' object has no attribute 'getBonification'

class Funcionario:

    def __init__(self, nome, salario, cpf):
        self.nome = nome
        self.salario = salario
        self.cpf = cpf

   def getBonificacao(self):
            return self.salario * 0.10


class Gerente(Funcionario):

    def getBonificacao(self):
        return super().getBonificacao + 1000


gF = Gerente("weliton", 312.22, "332322333")
print(gF.getBonificacao())
    
asked by anonymous 19.09.2017 / 00:19

1 answer

2

First, indentation is all in a Python code. A wrong white space and your code will not work: it will give you an error or it will generate some unexpected result. In the Funcionario class you start by using an indentation of 4 whitespace, so you must maintain constancy for the rest of the program. To define the method getBonificacao you used only 3 spaces and in the body of the method you used 8 spaces. Be consistent: always use 4 spaces.

class Funcionario:

    def __init__(self, nome, salario, cpf):
        self.nome = nome
        self.salario = salario
        self.cpf = cpf

    def getBonificacao(self):
        return self.salario * 0.10

In class Gerente , in the body of method getBonificacao , you did not call the same method of the parent class, but instead tried to add a reference to this method in 1000. This does not seem to make sense and to call the method properly you will need to add the parentheses.

class Gerente(Funcionario):

    def getBonificacao(self):
        return super().getBonificacao() + 1000

In this way, when running the program, it will have the result 1031.222 , which refers to the wage defined in the constructor multiplied by 0.10 and summed in 1000 .

  

See working at Ideone .

    
19.09.2017 / 00:53