Course of Raku / Objects, I/O, and exceptions / Classes and objects / Roles / Exercises

A named, aged pet

Problem

Define two roles:

  • Named, with a method label that returns I am <name>;
  • Aged, with a method status that returns <age> years.

Then define a class Pet that does both roles and has the attributes name and age. Create a pet named Rex aged 3, and print its label and its status.

Example

The program prints:

I am Rex
3 years

Solution

See the solution

Course navigation

Solution: A sized box   |   Solution: A named, aged pet