Herencia en Python

#!/usr/bin/env python # -*- coding: utf-8 -*- class Persona(object): def __init__(self,nombre,apellidos): self.nombre = nombre self.apellidos = apellidos print('Se creo e inicio objeto Persona') def __repr__(self): return str(self.__dict__) class Autor(Persona): def __init__(self, nombre, apellidos,telefono): Persona.__init__(self,nombre,apellidos) self.telefono = telefono print("Se creo e inicio objeto Autor") def __repr__(self): return str(self.__dict__) class Libro(object): def __init__(self, titulo, isbn,autor): self.titulo = titulo self.isbn = isbn self.autor = autor def __repr__(self): return str(self.__dict__) def main(): autor = Autor('Jose','Saramago','') print(autor) libros = [Libro('Ensayo sobre la lucidez','',autor), Libro('Ensayo sobre la ceguera','',autor),Libro('Manual de pintura y caligrafia','',autor),Libro('Cain','',autor)] for libro in libros: print(libro) if __name__ == '__main__': main()
Ejemplo de herencia en Python

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.