Skip to content

Instantly share code, notes, and snippets.

@Pasanpr
Created December 5, 2016 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pasanpr/88b729fbdde877ad23778f4cbbf0e3c8 to your computer and use it in GitHub Desktop.
Save Pasanpr/88b729fbdde877ad23778f4cbbf0e3c8 to your computer and use it in GitHub Desktop.
Example code for type casting introduction
class Employee {
let name: String
init(name: String) {
self.name = name
}
}
class HourlyEmployee: Employee {
let hourlyWage: Double
init(name: String, hourlyWage: Double) {
self.hourlyWage = hourlyWage
super.init(name: name)
}
func payWages(for hours: Double) -> Double {
return hourlyWage * hours
}
}
class SalariedEmployee: Employee {
let salary: Double
init(name: String, salary: Double) {
self.salary = salary
super.init(name: name)
}
func paySalary() -> Double {
return salary/24
}
}
let hourlyEmployee = HourlyEmployee(name: "Taylor", hourlyWage: 12.00)
let salariedEmployee = SalariedEmployee(name: "Lorenzo", salary: 62000)
let employees = [hourlyEmployee, salariedEmployee]
@mazurick
Copy link

mazurick commented Jul 9, 2019

This is awesome. Thanks Pasan!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment