相关疑难解决方法(0)

在 Raku 中混合私有和公共属性和访问器

#Private attribute example
class C { 
    has $!w;                            #private attribute
    multi method w { $!w }              #getter method
    multi method w ( $_ ) {                 #setter method
        warn “Don’t go changing my w!”;   #some side action
        $!w = $_
    }  
}
my $c = C.new
$c.w( 42 )
say $c.w #prints 42
$c.w: 43
say $c.w #prints 43

#but not
$c.w = 44
Cannot modify an immutable Int (43)
Run Code Online (Sandbox Code Playgroud)

到目前为止,如此合理,然后

#Public attribute example
class C { 
    has $.v is rw …
Run Code Online (Sandbox Code Playgroud)

raku

13
推荐指数
3
解决办法
272
查看次数

标签 统计

raku ×1