Last active
January 26, 2025 00:49
-
-
Save j2deme/268023349c32ee08a4113151bd673779 to your computer and use it in GitHub Desktop.
Polimorfismo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from abc import ABC, abstractmethod | |
from mixinMortalidad import MixinMortalidad | |
class Animal(ABC, MixinMortalidad): | |
def __init__(self, nombre="", edad=1): | |
self.nombre = nombre | |
self.edad = edad | |
@abstractmethod | |
def comer(self): | |
pass | |
def dormir(self): | |
print("El animal está durmiendo 💤") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from animal import Animal | |
class Gato(Animal): | |
def comer(self): | |
print("El gato está comiendo 🐟") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from animal import Animal | |
class Leon(Animal): | |
def comer(self): | |
print("El león esta comiendo 🦓") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from perro import Perro | |
from gato import Gato | |
from leon import Leon | |
from ornitorrinco import Ornitorrinco | |
from animal import Animal | |
def main(): | |
lomito = Perro() | |
michi = Gato() | |
alex = Leon() | |
perry = Ornitorrinco(nombre="Perry") | |
# animal = Animal() # No se puede instanciar una clase abstracta | |
lomito.comer() | |
lomito.dormir() | |
michi.comer() | |
michi.dormir() | |
print("== EJEMPLO ==") | |
# Uso del polimorfismo | |
# Mediante el uso del polimorfismo, podemos crear una lista de objetos de diferentes clases | |
# que heredan de la misma clase padre y en consecuencia, pueden ser tratados de la misma manera | |
animales = [lomito, michi, alex, perry] | |
for animal in animales: | |
animal.comer() # Método sobreescrito | |
animal.dormir() # Método heredado | |
animal.morir() # Método del Mixin | |
if __name__ == "__main__": | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from animal import Animal | |
class Mamifero(Animal): | |
def __init__(self, nombre="", edad=1, especie="Mamífero"): | |
super().__init__(nombre, edad) | |
self.especie = especie | |
def amamantar(self): | |
print("🍼") | |
def parir(self): | |
print("👶🏻") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MixinMortalidad: | |
def morir(self): | |
print("💀") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from mamifero import Mamifero | |
from oviparo import Oviparo | |
class Ornitorrinco(Mamifero, Oviparo): | |
def __init__(self, nombre="", edad=1): | |
super().__init__(nombre, edad) | |
self.NUMERO_HUEVOS = 0 | |
def comer(self): | |
print("🐞🐛🐚") | |
def morir(self): | |
print("Oh! me muero! 😵") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Oviparo: | |
NUMERO_HUEVOS = 0 | |
def poner_huevo(self): | |
print("🥚") | |
self.NUMERO_HUEVOS += 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from animal import Animal | |
class Perro(Animal): | |
def comer(self): | |
print("El perro está comiendo 🍖") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment