-
-
Save Pasanpr/88b729fbdde877ad23778f4cbbf0e3c8 to your computer and use it in GitHub Desktop.
Example code for type casting introduction
This file contains 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
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] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is awesome. Thanks Pasan!