informatica:programacion:python:poo
Diferencias
Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anteriorRevisión previaPróxima revisión | Revisión previa | ||
| informatica:programacion:python:poo [2021/02/10 16:18] – [Clases] tempwin | informatica:programacion:python:poo [2021/10/10 13:47] (actual) – [Recursos] tempwin | ||
|---|---|---|---|
| Línea 4: | Línea 4: | ||
| <WRAP center round info 60%> | <WRAP center round info 60%> | ||
| - | En Python todo es un objeto | + | En Python todo es un objeto. El tipo más básico de objeto en Python es '' |
| </ | </ | ||
| Línea 106: | Línea 106: | ||
| <code python> | <code python> | ||
| - | gorrion = pajaro(' | + | gorrion = pajaro(' |
| </ | </ | ||
| Línea 189: | Línea 189: | ||
| print(c1.ruedas) # 4 | print(c1.ruedas) # 4 | ||
| + | </ | ||
| + | |||
| + | Podemos modificar las propiedades del objeto: | ||
| + | |||
| + | <code python> | ||
| + | c1.ruedas = 6 | ||
| + | |||
| + | print(c1.ruedas) # 6 | ||
| </ | </ | ||
| ==== Métodos ==== | ==== Métodos ==== | ||
| Línea 226: | Línea 234: | ||
| # Ford | # Ford | ||
| </ | </ | ||
| + | |||
| + | ===== Herencia ===== | ||
| + | |||
| + | La herencia es un proceso que nos permite utilizar métodos definidos en otras clases sin tener que definirlos en nuestra clase. De esta manera se comparte la funcionalidad, | ||
| + | |||
| + | <code python> | ||
| + | class Coche(object): | ||
| + | |||
| + | def __init__(self): | ||
| + | print(" | ||
| + | | ||
| + | def arrancar(self): | ||
| + | print(" | ||
| + | |||
| + | def parar(self): | ||
| + | print(" | ||
| + | |||
| + | </ | ||
| + | |||
| + | Creamos una clase heredada de la anterior: | ||
| + | |||
| + | <code python> | ||
| + | class Renault(Coche): | ||
| + | |||
| + | def __init__(self): | ||
| + | # Llamamos al constructor de la clase padre | ||
| + | Car.__init__(self) | ||
| + | |||
| + | print(" | ||
| + | |||
| + | </ | ||
| + | |||
| + | Ahora, desde la clase **Renault**, | ||
| + | |||
| + | <code python> | ||
| + | r = Renault() | ||
| + | |||
| + | r.arrancar() | ||
| + | |||
| + | r.parar() | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Sobrescritura de métodos ==== | ||
| + | |||
| + | Si queremos redefinir algún método de la clase padre, basta con definirlo con el mismo nombre en la clase heredada y darle la funcionalidad que queramos: | ||
| + | |||
| + | <code python> | ||
| + | class Renault(Coche): | ||
| + | |||
| + | def __init__(self): | ||
| + | # Llamamos al constructor de la clase padre | ||
| + | Car.__init__(self) | ||
| + | |||
| + | print(" | ||
| + | |||
| + | def arrancar(self): | ||
| + | print(" | ||
| + | </ | ||
| + | |||
| + | Si quisiéramos hacer referencia a un método de la clase padre, utilizamos '' | ||
| + | |||
| + | <code python> | ||
| + | (...) | ||
| + | def arrancar(self): | ||
| + | super().arrancar() | ||
| + | print(" | ||
| + | </ | ||
| + | |||
| + | ===== Imprimir la representación de un objeto ===== | ||
| + | |||
| + | Python llama al método '' | ||
| + | |||
| + | Si queremos decidir qué imprimirá, crearemos nosotros un método '' | ||
| + | |||
| + | <code python> | ||
| + | class Coordenada(object): | ||
| + | def__init__(self, | ||
| + | self.x = x | ||
| + | self.y = y | ||
| + | | ||
| + | def __str__(self): | ||
| + | return "<" | ||
| + | </ | ||
| + | |||
| + | <WRAP center round important 60%> | ||
| + | '' | ||
| + | </ | ||
| + | |||
| + | ==== Operadores especiales ==== | ||
| + | |||
| + | Hay una serie de operadores que al utilizarlos en Python, llama a ciertos métodos de los objetos: | ||
| + | |||
| + | ^ Método | ||
| + | | '' | ||
| + | | '' | ||
| + | | '' | ||
| + | | '' | ||
| + | | '' | ||
| + | | '' | ||
| + | |||
| + | |||
| + | * https:// | ||
| + | |||
| + | ===== Recursos ===== | ||
| + | |||
| + | * [[https:// | ||
informatica/programacion/python/poo.1612970281.txt.gz · Última modificación: por tempwin
