Managing Instance data
AUTHOR
uzluisf
Problem
Each data of an object needs its method for access. How do you write methods that manipulate the object's instance data?
Solution
An attribute declared with the $.
have an automatically
generated read-only accessor method named after it. Tagging
the attribute with the is rw
trait makes the accessor method writable.
#!/usr/bin/env raku
use v6;
class Person {
has $.name is rw;
has $.age is rw;
}
my Person $p1 .= new: name => 'Sylvia', :age(24);
put $p1.name; #=> Sylvia
put $p1.age; #=> 24
$p1.name = 'Sylvester';
$p1.age = 23;
put $p1.name; #=> Sylvester
put $p1.age; #=> 23
# vim: expandtab shiftwidth=4 ft=perl6