# Contenedores
class Camion:
def __init__(self, c):
if isinstance(c, ContenedorGranCapacidad):
self.__class__ = Trailer
@staticmethod
def creaCamionPara(c):
if isinstance(c, ContenedorGranCapacidad):
return Trailer(c)
else:
return Camion(c)
def __str__(self):
return "camion"
class Trailer:
def __init__(self, c):
if isinstance(c, ContenedorNormal):
self.__class__ = Camion
def __str__(self):
return "trailer"
class Barco:
def __init__(self, contenedores):
self._contenedores = contenedores
def estaDescargado(self):
return len(self._contenedores) == 0
def descargaContenedor(self):
return self._contenedores.pop()
def __str__(self):
return str.join(",".join([str(x) for x in self._contenedores]))
class Contenedor:
pass
class ContenedorNormal(Contenedor):
def __str__(self):
return "contNormal"
class ContenedorGranCapacidad(Contenedor):
def __str__(self):
return "contGrande"
if __name__ == "__main__":
b = Barco([ContenedorNormal(), ContenedorGranCapacidad(), ContenedorNormal()])
while not (b.estaDescargado()):
# Uso de herencia dinamica
print(Camion(b.descargaContenedor()))
# Uso de método estático
# print(Camion.creaCamionPara(b.descargaContenedor()))
IyBDb250ZW5lZG9yZXMKCmNsYXNzIENhbWlvbjoKICAgIGRlZiBfX2luaXRfXyhzZWxmLCBjKToKICAgICAgICBpZiBpc2luc3RhbmNlKGMsIENvbnRlbmVkb3JHcmFuQ2FwYWNpZGFkKToKICAgICAgICAgICAgc2VsZi5fX2NsYXNzX18gPSBUcmFpbGVyCgogICAgQHN0YXRpY21ldGhvZAogICAgZGVmIGNyZWFDYW1pb25QYXJhKGMpOgogICAgICAgIGlmIGlzaW5zdGFuY2UoYywgQ29udGVuZWRvckdyYW5DYXBhY2lkYWQpOgogICAgICAgICAgICByZXR1cm4gVHJhaWxlcihjKQogICAgICAgIGVsc2U6CiAgICAgICAgICAgIHJldHVybiBDYW1pb24oYykKCiAgICBkZWYgX19zdHJfXyhzZWxmKToKICAgICAgICByZXR1cm4gImNhbWlvbiIKCgpjbGFzcyBUcmFpbGVyOgogICAgZGVmIF9faW5pdF9fKHNlbGYsIGMpOgogICAgICAgIGlmIGlza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