Skip to content

Instantly share code, notes, and snippets.

@Pasanpr Pasanpr/typeCasting.swift Secret
Created Dec 5, 2016

Embed
What would you like to do?
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

This comment has been minimized.

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
You can’t perform that action at this time.