No tienes acceso a esta clase

¡Continúa aprendiendo! Únete y comienza a potenciar tu carrera

Herencia

16/27
Recursos

Aportes 9

Preguntas 0

Ordenar por:

¿Quieres ver más aportes, preguntas y respuestas de la comunidad?

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.

dejo mi aporte, aplique tambien enumerados.

class WheeledVehicle {
    var currentSpeed = 0.0
    var brand : String = ""
    var model : String = ""
    var year : String = ""
    var doors = 0
    var wheels = 0
    var hasMotor = true
    var description: String {
        return "Viajando a \(currentSpeed)"
    }
    func makeNoise(){

    }
}

class FourWheelVehicle : WheeledVehicle {
    enum VelocityBox: Int{
        case automatic = 1, standar
    }
    enum EngineType: String{
        case octanos87, octanos92, hybrid, electric
    }
    enum KindOfCar : String{
        case city, highway, materialTransportation, publicTransportation, racing
    }
    var seating = 0
    var airConditioning = true
    var doubleTraction = false
    var gearbox = 5
    
    var velocityBox : VelocityBox
    var engineType : EngineType
    var kindOfCar: KindOfCar
    
    init(velocityBox : VelocityBox, engineType : EngineType, kindOfCar: KindOfCar) {
        self.velocityBox = velocityBox
        self.engineType = engineType
        self.kindOfCar = kindOfCar
    }
    
    
}

class Car : FourWheelVehicle {
    enum CarShape{
        case sedan, coupe, convertible, hatchback, suv, pickUp
        // SUV = Sport Utility Vehicle
    }
    var carShape: CarShape
    init(carShape: CarShape, velocityBox : VelocityBox, engineType : EngineType, kindOfCar: KindOfCar) {
        self.carShape = carShape
        super.init(velocityBox : velocityBox, engineType : engineType, kindOfCar: kindOfCar)
    }
    
}

let peugeot301 = Car(carShape: .sedan, velocityBox: .automatic, engineType: .octanos92, kindOfCar: .city)
peugeot301.seating = 5
peugeot301.currentSpeed = 100.0
peugeot301.brand = "Peugeot"
peugeot301.model = "301"
peugeot301.year = "2018"
peugeot301.doors = 5
peugeot301.wheels = 4

print("\(peugeot301.brand) \(peugeot301.model) \(peugeot301.year) \(peugeot301.description)")

let jeepAvenger4x4 = Car(carShape: .suv, velocityBox: .standar, engineType: .hybrid, kindOfCar: .highway)
jeepAvenger4x4.seating = 4
jeepAvenger4x4.currentSpeed = 200.90
jeepAvenger4x4.brand = "Jeep"
jeepAvenger4x4.model = "Avenger 4x4"
jeepAvenger4x4.year = "2024"
jeepAvenger4x4.doors = 4
jeepAvenger4x4.wheels = 4
jeepAvenger4x4.doubleTraction = true

print("\(jeepAvenger4x4.brand) \(jeepAvenger4x4.model) \(jeepAvenger4x4.year) \(jeepAvenger4x4.description)")

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
}