Course of Raku / Objects, I/O, and exceptions / Classes and objects / Inheritance / Exercises / Employees and roles
Solution: Employees and roles
Here is a possible solution to the task.
Code
class Employee {
has $.name;
method role {
'staff';
}
method badge {
"$.name - " ~ self.role;
}
}
class Manager is Employee {
method role { 'manager' }
}
class Intern is Employee {
method role { 'intern' }
}
say Manager.new(name => 'Anna').badge;
say Intern.new(name => 'Bob').badge;🦋 You can find the source code in the file dog-and-cat.raku.
Output
Anna - manager
Bob - internComments
ManagerandInterninherit thenameattribute and thebadgemethod fromEmployee, so neither of them needs to repeat that code.Each child overrides
role. Becausebadgecallsself.role, it picks up the overriding version belonging to the actual object, giving a different badge for the manager and the intern.The name of the method,
role, is just a name. But it coincides with the keywordrolethat will be introduced later.