Skip to content

Instantly share code, notes, and snippets.

@mayooresan

mayooresan/war.swift

Created Aug 11, 2017
Embed
What would you like to do?
protocol WarCapable {
var topSpeed : Double {get set}
var shotsPerMinute : Int {get set}
}
protocol SpyCapable {
var numberOfCameras : Int {get set}
}
class WarTank : WarCapable {
var topSpeed: Double = 100.0
var shotsPerMinute: Int = 5
}
class SpyJeep : WarCapable, SpyCapable{
var topSpeed: Double = 200
var shotsPerMinute: Int = 1
var numberOfCameras: Int = 5
}
class SpyBike : SpyCapable{
var numberOfCameras: Int = 1
}
//War tank objects
var t10 = WarTank()
var t11 = WarTank()
t11.topSpeed = 110.0
//Spy Jeep Objects
var sj10 = SpyJeep()
var sj11 = SpyJeep()
sj11.topSpeed = 190.0
// Spy Bike Objects
var sp10 = SpyBike()
var sp11 = SpyBike()
sp11.numberOfCameras = 2
// Extensions
extension WarCapable{
func canTravelFaster(item : WarCapable)->Bool{
return self.topSpeed > item.topSpeed
}
}
t10.canTravelFaster(item: t11)
// returns false
sj11.canTravelFaster(item: t11)
// returns true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment