Polimorfismo de sobreposição - Python Orientado a Objetos
This commit is contained in:
39
aula144.py
39
aula144.py
@@ -11,3 +11,42 @@
|
||||
# Princípio da substituição de liskov
|
||||
# Objetos de uma superclasse devem ser substituíveis
|
||||
# por objetos de uma subclasse sem quebrar a aplicação.
|
||||
# Sobrecarga de métodos (overload) 🐍 = ❌
|
||||
# Sobreposição de métodos (override) 🐍 = ✅
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
|
||||
class Notificacao(ABC):
|
||||
def __init__(self, mensagem):
|
||||
self.mensagem = mensagem
|
||||
|
||||
@abstractmethod
|
||||
def enviar(self) -> bool: ...
|
||||
|
||||
|
||||
class NotificacaoEmail(Notificacao):
|
||||
def enviar(self) -> bool:
|
||||
print('E-mail: enviando - ', self.mensagem)
|
||||
return True
|
||||
|
||||
|
||||
class NotificacaoSMS(Notificacao):
|
||||
def enviar(self) -> bool:
|
||||
print('SMS: enviando - ', self.mensagem)
|
||||
return False
|
||||
|
||||
|
||||
def notificar(notificacao: Notificacao):
|
||||
notificacao_enviada = notificacao.enviar()
|
||||
|
||||
if notificacao_enviada:
|
||||
print('Notificação enviada')
|
||||
else:
|
||||
print('Notificação NÃO enviada')
|
||||
|
||||
|
||||
notificacao_email = NotificacaoEmail('testando e-mail')
|
||||
notificar(notificacao_email)
|
||||
|
||||
notificacao_sms = NotificacaoSMS('testando SMS')
|
||||
notificar(notificacao_sms)
|
||||
|
||||
Reference in New Issue
Block a user