Inheritance and polymorphism in python

Asked

Viewed 832 times

-2

inserir a descrição da imagem aqui

from random import randint

class Nomes(object):
    def __init__(self, qtd_letras):
        self.letras = qtd_letras
        self.alfabeto = (('A', 'E', 'I', 'O', 'U'), ('A','B', 'C', 'D','E','F','G','H','I','J','K', 'L','M','N','O','P','Q', 'R','S','T','U','V','W', 'X', 'Y', 'Z'))
    def nome(self):
        nome = ''
        while True:
            aleat_letra = randint(0, len(self.alfabeto[1]) - 1)
            nome += self.alfabeto[1][aleat_letra]
            aleat_letra = randint(0, len(self.alfabeto[0]) - 1)
            nome += self.alfabeto[0][aleat_letra]
            if len(nome) >= self.letras:
                nome = nome[0:self.letras]
                break
        return nome


class Sobrenomes(Nomes):
    def __init__(self, qtd_letras):
        super(Sobrenomes, self).__init__(qtd_letras)
    def sobrenomes(self):
        super(Sobrenomes, self).nome()


a = Nomes(5)
print(a.nome())

b = Sobrenomes(5)
print(b.sobrenome())
Caio
None

This code has 02 classes, they are the class Names and the class Surnames (and the class Surnames inherits all the characteristics of the class Name)

When I enter the Names class and use the method . name():

>>> meu_nome =  Nomes(6)
>>> print(meu_nome.nome())
>>> Carlos

But when I use it with class Surnames complicates:

>>> meu_sobrenome = Sobrenomes(8)
>>> print(meu_sobrenome.sobrenome())
>>> none

The value returned is None but... the Surnames class inherits everything from the Names class! the only change was in the name of the only method but even so, the method surname of the class Surnames inherits the characteristics of the method name of the mother class Names. I don’t get it anymore.

  • Have you ever tried to run over come with parameter 6 to see if the result will be the same?

2 answers

3

The only thing I had to change in your code to work was

def sobrenomes(self):
  return super(Sobrenomes, self).nome()

I had to put the return in the "last names" method, and put the "s" in the "last name" method since its method is plural

from random import randint

class Nomes(object):

    def __init__(self, qtd_letras):
        self.letras = qtd_letras
        self.alfabeto = (('A', 'E', 'I', 'O', 'U'), ('A','B', 'C', 'D','E','F','G','H','I','J','K', 'L','M','N','O','P','Q', 'R','S','T','U','V','W', 'X', 'Y', 'Z'))

    def nome(self):
        nome = ''
        while True:
            aleat_letra = randint(0, len(self.alfabeto[1]) - 1)
            nome += self.alfabeto[1][aleat_letra]
            aleat_letra = randint(0, len(self.alfabeto[0]) - 1)
            nome += self.alfabeto[0][aleat_letra]
            if len(nome) >= self.letras:
                nome = nome[0:self.letras]
                break
        return nome


class Sobrenomes(Nomes):

    def __init__(self, qtd_letras):
        super(Sobrenomes, self).__init__(qtd_letras)

    def sobrenomes(self):
      return super(Sobrenomes, self).nome()


a = Nomes(5)
print(a.nome())

b = Sobrenomes(5)
print(b.sobrenomes())

Execute

0

In function sobrenome is missing the return

    def sobrenome(self):
        return super(Sobrenomes, self).nome()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.