Iván Chillón
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # PRO-UT2-A4 Herencia múltiple ## Herencia múltiple en Python Python permite herencia múltiple. Se utiliza cuando la clase hija tiene características de **más de una clase madre**. En este caso, la clase hija hereda los miembres de más de una clase madre: ```python class MotherClass1: pass class MotherClass2: pass class ChildClass(MotherClass1, MotherClass2): pass ``` Ejemplo: ```python class SeaAnimal: def __init__(self, name): self.name = name def swim(self): return "Swimming" class LandAnimal: def __init__(self, name): self.name = name def walk(self): return "Walking..." class Penguin(SeaAnimal, LandAnimal): def __init__(self, name): super().__init__(name) ``` En el caso anterior un Pingüino **es un** animal marino y **es un** animal terrestre y puede, por tanto, nadar y caminar. ```python linux = Penguin("Tux") print(linux.walk()) # Walking... print(linux.swim()) # Swimming ``` ## Orden de resolución Al heredar de más de una clase madre se puede dar el caso de que dichas clases madres tengan métodos con el mismo nombre: ```python class B: def x(self): print('x: B') class C: def x(self): print('x: C') class D(B, C): pass d = D() d.x() ``` El resultado de ejecutar el programa anterior es: ``` x: B ``` Python resuelve este caso siguiendo las siguientes reglas: * Si el método ha sido sobreescrito en la clase hija, este es el que se ejecuta. * Si el método no existe en la clase hija pero si en más de una de las clases madres se ejecuta el de aquella que se haya especificado antes a la hora de heredar. Lo mismo pasa si usamos para llamar usando `super()` un método que existe en más de una clase madre, se resuelve ejecutando el de aquella que se haya indicando que **hereda en primer lugar** (más a la izquierda): ```python class B: def x(self): print('x: B') class C: def x(self): print('x: C') class D(B, C): def x(self): super().x() pass d = D() d.x() # 'x: B' ``` ## Usando métodos de más de una clase madre Cómo vimos, el método `super()` nos perminte acceder a métodos de la clase madre, pero si tenemos más de un antecesor con **métodos con el mismo nombre**, debemos disponer de una forma de discriminar de cuál de ellos queremos reutilizar un miembro. Para ello reemplazamos el método `super()` por el nombre de la clase de la que queremos reutilizar el método: ```python class B: def x(self): print('x: B') class C: def x(self): print('x: C') class D(B, C): def x(self): C.x(self) # Se ha de pasar self al método pass d = D() d.x() # x: C ``` Además, cuando invocamos de esta forma un método de una de las clases madres hemos pasar como primer parámetro del mismo `self` para que la clase madre disponga del objeto sobre el que aplicar el método. El funcionamiento es el mismo para cualquier método, en particular también para el constructor: ```python class SeaAnimal: def __init__(self, name): print("SeaAnimal INIT") self.name = name def swim(self): return "Swimming..." class LandAnimal: def __init__(self, age): print("LandAnimal INIT") self.age = age def walk(self): print("Walking...") class Penguin(SeaAnimal, LandAnimal): def __init__(self, name, age): SeaAnimal.__init__(self, name=name) LandAnimal.__init__(self, age=age) penguin = Penguin("Kowalski", 3) print(penguin.name, penguin.age) ``` ### Actividad 1 Crea la clase `CocheHibrido` que hereda de las clases `CocheCombustion` y `CocheElectrico` que vimos en la actividad anterior ```python class Vehiculo: def __init__(self, matricula, color, num_ruedas): self._matricula = matricula self._color = color self._num_ruedas = num_ruedas def get_matricula(self): return self._matricula def __str__(self): return f"Vehículo con matrícula {self.get_matricula()}, de color {self._color} y {self._num_ruedas} ruedas." class CocheCombustion(Vehiculo): def __init__(self, matricula, color, num_ruedas, cilindrada): super().__init__(matricula, color, num_ruedas) self._cilindrada = cilindrada def get_cilindrada(self): return self._cilindrada def descripcion(self): return f"Coche con motor de combustión de {self.get_cilindrada()} cc, matrícula {self.get_matricula()}, de color {self._color} y {self._num_ruedas} ruedas." class CocheElectrico(Vehiculo): def __init__(self, matricula, color, num_ruedas, potencia): super().__init__(matricula, color, num_ruedas) self._potencia = potencia def get_potencia(self): return self._potencia def descripcion(self): return f"Coche con motor eléctrico de {self.get_potencia()}CV, matrícula {self.get_matricula()}, de color {self._color} y {self._num_ruedas} ruedas." class Moto(Vehiculo): def descripcion(self): return f"Moto, matrícula {self.get_matricula()}, de color {self._color} y {self._num_ruedas} ruedas." ``` Reescribe los métodos de la clase `CocheHibrido` de forma que el resultado de los mismos sea coherente. Aprovecha los métodos de las clases madre siempre que sea posible. Inserta el resultado: ```python class Vehiculo: def __init__(self, matricula, color, num_ruedas): self._matricula = matricula self._color = color self._num_ruedas = num_ruedas def get_matricula(self): return self._matricula def __str__(self): return f"Vehículo con matrícula {self.get_matricula()}, de color {self._color} y {self._num_ruedas} ruedas." class CocheCombustion(Vehiculo): def __init__(self, matricula, color, num_ruedas, cilindrada): Vehiculo.__init__(self, matricula, color, num_ruedas) self._cilindrada = cilindrada def get_cilindrada(self): return self._cilindrada def descripcion(self): return f"Coche con motor de combustión de {self.get_cilindrada()} cc, matrícula {self.get_matricula()}, de color {self._color} y {self._num_ruedas} ruedas." class CocheElectrico(Vehiculo): def __init__(self, matricula, color, num_ruedas, potencia): super().__init__(matricula, color, num_ruedas) self._potencia = potencia def get_potencia(self): return self._potencia def descripcion(self): return f"Coche con motor eléctrico de {self.get_potencia()}CV, matrícula {self.get_matricula()}, de color {self._color} y {self._num_ruedas} ruedas." class Moto(Vehiculo): def descripcion(self): return f"Moto, matrícula {self.get_matricula()}, de color {self._color} y {self._num_ruedas} ruedas." class CocheHibrido(CocheCombustion, CocheElectrico): def __init__(self, matricula, color, num_ruedas, cilindrada, potencia): CocheCombustion.__init__(self, matricula, color, num_ruedas, cilindrada) self._potencia = potencia def __str__(self): result = super().__str__() result += f"\n Una cilindrada de {self._cilindrada} CC y una potencia de {self._potencia} CV" return result ch = CocheHibrido("1234GHJ", "Gris", 4, 1500, 230) print(ch) ``` ### Actividad 2 La clase `Venta` tiene las siguientes propiedades: * Atributos: * Lista de listas con datos de la compra de la forma `[['peras', 2],['manzanas', 1.5], ['tomates', 3]]` en la que el primer elemento es el nombre del producto y el segundo la cantidad * Métodos: * `__init__()` se le pasa una lista de artículos * `__str__()` muestra resumen de la lista de artículos * `agregar_pedido(producto, cantidad)` se añade el producto a la lista si no existe. Si existe se incrementa la cantidad: * `modificar_pedido(producto, cantidad)` se modifica la cantidad de un producto si está en la lista. * `quitar_pedido(producto)` se elimina el producto de la lista. * `_obtener_cantidad(producto)` se le pasa el nombre de un producto y devuelve la cantidad del mismo La clase `Productos` tiene las siguientes propiedades: * Atributos: * Lista de listas de artículos con su precio de la forma `[['aguacates', 2.67],['manzanas', 1.13],['peras', 1.43],['tomates', 0.80]]` en la que el primer elemento es el producto y el segundo su precio * Métodos: * `__init__()` se le pasa una lista artículos con su precio * `__str__()` * `cambiar_precio(producto, precio)` * `quitar_producto(producto)` se elimina un producto de la lista de productos * `obtener_precio(producto)` * `_existe_producto(producto)` devuelve True si el producto está en la lista y False en caso contrario. La clase `Factura` hereda de `Productos` y de `Venta` y tiene las siguientes propiedades: * Atributos: * Lista con datos de la compra * lista con precios de los artículos * nif del cliente * Métodos: * Los heredados de las clases **Madre** * `__init__()` recibe todos los atributos. Aprovecha constructores `Productos` y `Venta` * `_generar_encabezado()` devuelve HTML con fila de encabezado con columnas **Producto | Cantidad | Precio unitario | Precio total** * `_generar_fila(producto, cantidad, precio)` devuelve HMLT con fila con dato de uno de los productos de la factura * `_calcular_total()` devuelve el total de la factura * `_generar_total()` devuelve HTML con la fila que muestra el total de la misma. * `mostrar_factura()` devuelve tabla HTML completa con los datos de la compra. Aprovecha los métodos `_generar_encabezado(), _generar_fila() y _generar_total()` ###### tags: `pro` `ut2` `herencia` `múltiple` `oop` `poo`

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully