No tienes acceso a esta clase

¬°Contin√ļa aprendiendo! √önete y comienza a potenciar tu carrera

Herencia

16/27
Recursos

Aportes 8

Preguntas 0

Ordenar por:

¬ŅQuieres ver m√°s aportes, preguntas y respuestas de la comunidad?

o inicia sesión.

class Car : Vehicle{
    enum KindOfCar{
    case electric, combustion
    }
    
    var kindOfCar = KindOfCar.self
    var maxSpeed = 0.0
}

class ElectricCar : Car{
    var autonomy = 0.0
}

class CombustionCar : Car{
    var performance = 0.0
}

let electricCar = ElectricCar()
electricCar.kindOfCar.electric
electricCar.maxSpeed = 220
electricCar.autonomy = 450

let combustionCar = CombustionCar()
combustionCar.kindOfCar.combustion
combustionCar.maxSpeed = 200
combustionCar.performance = 8

He complementado este curso con la lectura de algunos libros, entre ellos tengo este: Sumergete en los patrones de dise√Īo
Me ha ayudado a entender la POO

Creo que la programación orientada a protocolos (POP) es la mejor opción a OOP.

Busque como, desde el hijo, sobrescribir una var que proviene del padre y descubrí override

class Vehicle {
    var currentSpeed = 0.0
    var typeOfVehicle = ""
    var color = ""
    var description: String {
        return "Your \(typeOfVehicle):"
    }
    func makeNoise(){
        // empty
        print("El ruido depende del vehículo")
    }
}

class Car : Vehicle {
    var band: String
    var version: String
    var power: String
    var year: Int
    var doors: Int
    override var description: String {
        return "\(typeOfVehicle): \(band) \(version). Model: \(year). Color: \(color), with \(doors) doors and \(power) Horse Power."
    }
    
    init(band: String, version: String, power: String, year: Int, doors: Int) {
        self.band = band
        self.version = version
        self.power = power
        self.year = year
        self.doors = doors
        super.init() // Llama al inicializador de la clase base (Vehicle)
    }
}

class Bicycle : Vehicle { // herencia
    var material: String
    var hasBasket = false // basket = cesta
    var conditionBrakes: String
    override var description: String {
        return "\(typeOfVehicle): Material: \(material). Color: \(color). The brakes condition is \(conditionBrakes)."
    }
    
    init(material: String, hasBasket: Bool, conditionBrakes: String) {
        self.material = material
        self.hasBasket = hasBasket
        self.conditionBrakes = conditionBrakes
        super.init() // Llama al inicializador de la clase base (Vehicle)
    }
}

let mustang = Car(band: "Ford Mustang", version: "5.0 Gt", power: "426", year: 2017, doors: 2)
let bicy = Bicycle(material: "Aluminum", hasBasket: true, conditionBrakes: "good")

mustang.typeOfVehicle = "Car"
mustang.color = "White"

bicy.color = "blue"
bicy.typeOfVehicle = "Bicycle"
bicy.conditionBrakes = "good"

print(mustang.description)
// Car: Ford Mustang 5.0 Gt. Model: 2017. Color: White, with 2 doors and 426 Horse Power.
print(bicy.description)
// Bicycle: Material: Aluminum. Color: blue. The brakes condition is good.

reto terminado


class vehicle{
    var mark = ""
    var model = ""
    var year = 0
    
    var description: String{
        return "vehiculo marca: \(mark), modelo: \(model), a√Īo: \(year), "
    }
}

class bicycle: vehicle{
    var passager = 0
    var canasta = false
}


class AutoMovil: vehicle{
    var passager = 0
    var type = ""
    
    var descriptionDetalle : String{
        return "pasajeros: \(passager), tipo de automovil: \(type)"
    }
}

//creando un tesla
let Tesla = AutoMovil()

Tesla.mark = "Tesla"
Tesla.model = "S"
Tesla.year = 2021
Tesla.passager = 4
Tesla.type = "Electrico"

print("\(Tesla.description) \(Tesla.descriptionDetalle)")


//creando un mazda

let Mazda = AutoMovil()
Mazda.mark = "Mazda"
Mazda.model = "3"
Mazda.year = 2021
Mazda.passager = 2
Mazda.type = "combustible"

print("\(Mazda.description) \(Mazda.descriptionDetalle)")

//creando una bicicleta

let bici = bicycle()

bici.passager = 1
bici.mark = "cremas"
bici.model = "tradicional"
bici.year = 2021
bici.canasta = true

print(bici.description)

class motorCycle : Vehicle {
    var hasMotor = true
}

class gasCar : Vehicle {
    var needsGas = true
}

class boat : Vehicle {
    var waterVehicle = true
}

class formulaOne : gasCar {
    var maxSpeed = 400
}

class Tank : gasCar {
    var guns = 2
}

class familyCar : gasCar {
    var seats = 5
}

class electricBike : motorCycle {
    var homePlug = true
}

class gasBike : motorCycle {
    var tankCapacity = 5
}

class yatch : boat{
    var luxuryLevel = 100
}

class cruise : boat{
    var capacity = 500
}