Skip to content

Instantly share code, notes, and snippets.

@davidseek
Last active February 8, 2021 21:45
Show Gist options
  • Save davidseek/4650d19184cb9b07f476425cbd9d27a2 to your computer and use it in GitHub Desktop.
Save davidseek/4650d19184cb9b07f476425cbd9d27a2 to your computer and use it in GitHub Desktop.
human.swift
class Human {
// The eat function returns an indicator,
// whether or not the food has been eaten.
// Our human has free choice.
func eat(_ food: Food) -> Bool {
// But they're a simple person,
// that eats everything that's edible.
return food.isEdible
}
}
// Now we want to define a sub-class of Human
// to give our objects a more detailed definition.
class Vegan: Human {
// We're overriding eat to change the output.
override func eat(_ food: Food) -> Bool {
// Our Vegan only eats edible food that's vegan.
return food.isEdible && food.isVegan
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment