Python - Object-Oriented Programming

Asked

Viewed 65 times

2

I created a module with two classes: I want to use both as if the mouse depended on the computer. E.g.: when using them: only when the Computer is turned on does the mouse have permission to turn on (plugged in).. otherwise display a message that he cannot plug in...

But I did not succeed as shown in the code below:

Can someone help me and tell me where I went wrong, please?

from time import sleep


class Computador:
    def __init__(self, marca, ligado=False, desligado=False):
        self.marca = marca
        self.ligado = ligado
        self.desligado = desligado

    def ligar(self):
        if self.ligado:
            print(f'{self.marca}, já está ligado')
            return

        print(f'{self.marca} está ligado')
        self.ligado = True

    def desligar(self):
        if self.desligado:
            print(f'{self.marca}, já está desligado')
            return

        print(f'{self.marca} está desligado')
        self.desligado = True


class Mouse(Computador):
    def __init__(self, marca):
        super().__init__(marca)
        self.marca = marca
        self.plugado = False
        self.desplugado = False

    def plugar(self):
        if not self.desligado or not self.ligado:
            print(f'O {self.marca} não está conectando o mouse..')
            return

        print(f'{self.marca} está conectando...')
        sleep(1)
        print(f'{self.marca} plugando...')
        sleep(2)
        print('Pronto!')
        sleep(1)

    def desplugar(self):
        return


# Essa é a Segunda Parte do Código onde estou chamando os objetos/funções:

from classes import Computador, Mouse

c1 = Computador('Dell', 'Mouse RedDragon')
m1 = Mouse('RedDragon')
print(f'Descrição: [1]{c1.marca}\n')
m1.plugar()
c1.ligar()
c1.desligar()
c1.desligar()
c1.ligar()
m1.plugar()

Show me what I’m not doing right, please.

  • Thank you very much for the edition.

  • What would be the level of this content, since it is not Beginner Wallace Maxters , could tell me to edit, please??

1 answer

4


First, eliminate this class hierarchy:

class Mouse(Computador)

Here you are saying that Mouse is a sub-class of Computador, which makes no sense. A mouse is a computer? No, so this inheritance is wrong (read here, here, here and here to better understand).

Another point is that when plugging the mouse, you should tell where it is being plugged in. That is, the method plugar should receive a computer as a parameter, or the other way around: the computer could have a method plugar which receives the peripheral being plugged as parameter (in the example below I chose the second option).

And the computer could have a list of the peripherals plugged in, so you’ll know what’s already there.

On the computer, it makes no sense to have 2 variables, one for "on" and one for "off". Use only one with the value True or False. For example, you could have just ligado: if the value is True, is on, if it’s False, is off.

The way you did, it can result in some pretty weird stuff, since you did it:

def __init__(self, marca, ligado=False, desligado=False):

I mean, if I create Computador('marca'), so much ligado how much desligado evening False (that is, the computer is both on and off).

Anyway, an alternative would be:

class Computador:
    def __init__(self, marca, ligado=False, perifericos_plugados=None):
        self.marca = marca
        self.ligado = ligado
        # lista dos periféricos plugados (usar set que não permite elementos repetidos)
        if perifericos_plugados is None:
            self.perifericos = set()
        else:
            self.perifericos = set(perifericos_plugados)

    def ligar(self):
        if self.ligado:
            print(f'{self.marca}, já está ligado')
            return

        print(f'{self.marca} está ligado')
        self.ligado = True

    def desligar(self):
        if not self.ligado:
            print(f'{self.marca}, já está desligado')
            return

        print(f'{self.marca} está desligado')
        self.ligado = False

    def plugar(self, periferico):
        if not self.ligado:
            print('Computador desligado, não pode plugar')
            return

        if periferico in self.perifericos:
            print(f'Periférico {periferico.marca} já está plugado')
            return

        print(f'Plugando {periferico.marca} em {self.marca}')
        self.perifericos.add(periferico)

    def desplugar(self, periferico):
        if not self.ligado:
            print('Computador desligado, não pode desplugar')
            return

        if periferico not in self.perifericos:
            print(f'Periférico {periferico.marca} não está plugado')
            return

        print(f'Desplugando {periferico.marca} de {self.marca}')
        self.perifericos.remove(periferico)


class Mouse:
    def __init__(self, marca):
        self.marca = marca

c1 = Computador('Dell')
m1 = Mouse('RedDragon')
print(f'Descrição: [1]{c1.marca}\n')
c1.plugar(m1)
c1.ligar()
c1.desligar()
c1.desligar()
c1.ligar()
c1.plugar(m1)

Note that now the computer receives the peripheral to be plugged in and internally it makes the necessary validations (if it is connected, if the peripheral is already plugged in, etc).

From there you can put whatever logic you want in each case.

Browser other questions tagged

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