directions_car

Sistema de Vehículos: Múltiples Interfaces

Definición de Interfaces

battery_charging_full

Interface Electrico

interface Electrico {
    val bateria: Int
    fun cargar()
    fun nivelBateria(): Int
}
gps_fixed

Interface ConGPS

interface ConGPS {
    fun obtenerUbicacion(): String
    fun establecerDestino(destino: String)
}

info Cada interface define un comportamiento específico

Implementación Múltiple

directions_car

Clase CocheElectrico

class CocheElectrico(val modelo: String) : Electrico, ConGPS {
    override var bateria = 0
    private var ubicacion = "Desconocida"
    private var destino = ""
    override fun cargar() {
        bateria = 100
        println("⚡ $modelo cargado al 100%")
    }
    override fun nivelBateria() = bateria
    override fun obtenerUbicacion() = ubicacion
    override fun establecerDestino(dest: String) {
        destino = dest
        println("📍 Destino: $destino")
    }
}

play_arrow Ejemplo de uso:

val tesla = CocheElectrico("Tesla Model 3")
tesla.cargar()
tesla.establecerDestino("Barcelona")