Created
July 29, 2019 09:33
-
-
Save biloshkurskyi-ss/2cf8734b53790523e5022e5c8d326241 to your computer and use it in GitHub Desktop.
OOP with Swift examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
class Vehicle { | |
var model: String | |
var make: Int | |
init(model: String, make: Int) { | |
self.model = model | |
self.make = make | |
} | |
func start() { print("Vehicle - Start") } | |
func stop() { print("Vehicle - Stop") } | |
} | |
let сuriosity = Vehicle(model: "Rover", make: 2011) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
class Vehicle { | |
var model: String | |
var make: Int | |
init(model: String, make: Int) { | |
self.model = model | |
self.make = make | |
} | |
func start() { print("Vehicle - Start") } | |
func stop() { print("Vehicle - Stop") } | |
} | |
let сuriosity = Vehicle(model: "Rover", make: 2011) | |
class Car: Vehicle { | |
var tireCount: Int | |
private var isEngineWorks = false | |
init(model: String, make: Int, tireCount: Int = 4) { | |
self.tireCount = tireCount | |
super.init(model: model, make: make) | |
} | |
override func start() { | |
print("Car - Start") | |
} | |
override func stop() { | |
super.stop() | |
} | |
private func startEngine() { | |
isEngineWorks = true | |
} | |
private func stopEngine() { | |
isEngineWorks = false | |
} | |
} | |
let ford = Car(model: "Ford focus", make: 2018) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
class Vehicle { | |
var model: String | |
var make: Int | |
init(model: String, make: Int) { | |
self.model = model | |
self.make = make | |
} | |
func start() { print("Vehicle - Start") } | |
func stop() { print("Vehicle - Stop") } | |
} | |
let сuriosity = Vehicle(model: "Rover", make: 2011) | |
class Car: Vehicle { | |
var tireCount: Int | |
init(model: String, make: Int, tireCount: Int = 4) { | |
self.tireCount = tireCount | |
super.init(model: model, make: make) | |
} | |
override func start() { | |
print("Car - Start") | |
} | |
override func stop() { | |
super.stop() | |
} | |
} | |
let ford = Car(model: "Ford focus", make: 2018) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
class Vehicle { | |
var model: String | |
var make: Int | |
init(model: String, make: Int) { | |
self.model = model | |
self.make = make | |
} | |
func start() { print("Vehicle - Start") } | |
func stop() { print("Vehicle - Stop") } | |
} | |
class Car: Vehicle { | |
var tireCount: Int | |
private var isEngineWorks = false { | |
didSet { | |
print("Engine working: \(isEngineWorks)") | |
} | |
} | |
init(model: String, make: Int, tireCount: Int = 4) { | |
self.tireCount = tireCount | |
super.init(model: model, make: make) | |
} | |
override func start() { | |
startEngine() | |
print("Car - Start") | |
} | |
override func stop() { | |
super.stop() | |
stopEngine() | |
} | |
private func startEngine() { | |
isEngineWorks = true | |
} | |
private func stopEngine() { | |
isEngineWorks = false | |
} | |
} | |
class Bike: Vehicle { | |
func getInforation() -> String { | |
return "Bike model: \(model) maked: \(make)" | |
} | |
} | |
let сuriosity = Vehicle(model: "Rover", make: 2011) | |
let bike = Bike(model: "Pinarello", make: 2019) | |
let ford = Car(model: "Ford focus", make: 2018) | |
let vechicles = [сuriosity, bike, ford] | |
vechicles.forEach { vechicle in | |
print("Class: \(String(describing: vechicle)) model: \(vechicle.model)") | |
vechicle.start() | |
vechicle.stop() | |
print("\n") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment