Last active
March 10, 2017 06:11
-
-
Save paulw11/07aeeb62b4e00b599c9fe4fbca24eea5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
enum Sex { | |
case Male | |
case Female | |
case Other | |
} | |
enum Size:Int { | |
case ExtraSmall = 0 | |
case Small = 1 | |
case Medium = 2 | |
case Large = 3 | |
case ExtraLarge = 4 | |
} | |
enum Diet { | |
case Herbivore | |
case Carnivore | |
} | |
protocol Swimmable: class { | |
var swimSpeed: Double { set get } | |
} | |
protocol NeedsEnclosure { | |
func enclosureSize() -> Double | |
} | |
protocol Carnivorous { | |
func meatSize() -> Double | |
} | |
protocol Mammal { } | |
protocol Fish { | |
var adjustedSpeed: Double { set get } | |
} | |
protocol Bird { } | |
protocol Reptile { } | |
protocol Invertebrate { } | |
class Animal { | |
let sex: Sex | |
let size: Size | |
let diet: Diet | |
var weight: Double | |
var age: Int | |
init(sex: Sex, size: Size, diet: Diet, weight: Double, age: Int) { | |
self.sex = sex | |
self.size = size | |
self.diet = diet | |
self.weight = weight | |
self.age = age | |
} | |
} | |
class Carnivore: Animal, NeedsEnclosure { | |
init(sex: Sex, size: Size, weight: Double, age: Int) { | |
super.init(sex: sex, size: size, diet: .Carnivore, weight: weight, age: age) | |
} | |
} | |
extension NeedsEnclosure where Self: Animal { | |
func enclosureSize() -> Double { | |
return Double(size.rawValue) * weight | |
} | |
} | |
extension Carnivorous where Self: Carnivore { | |
func meatSize() -> Double { | |
return Double(size.rawValue) * weight | |
} | |
} | |
extension Swimmable where Self: Animal { | |
var adjustedSpeed: Double { return (swimSpeed/weight)/Double(age) } | |
} | |
class BigCat: Carnivore, Mammal, Swimmable, Carnivorous { | |
internal var swimSpeed: Double | |
init(sex: Sex, weight: Double, age: Int, swimSpeed: Double) { | |
self.swimSpeed = swimSpeed | |
super.init(sex: sex, size: .Large, weight: weight, age: age) | |
} | |
} | |
let tiger = BigCat(sex: .Female, weight: 120, age: 6, swimSpeed: 10) | |
print(tiger.size) | |
print(tiger.weight) | |
print(tiger.enclosureSize()) | |
print(tiger.swimSpeed) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment