
262
|
第
13
章
练习
13.4
将
Lepidoptera
角色、
CommonName
角色和
Butterfly
类分别存储在自己的文件中。
在创建
Butterfly
对象的程序中加载这些文件。使下面这个程序能正常工作:
use Butterfly;
my $butterfly = Butterfly.new:
:family( 'Nymphalidae' ),
:genus( 'Hamadryas' ),
:species( 'perlicus' ),
;
put $butterfly.binomial-name;
13.3
消除角色冲突
如果两个角色试图插入相同的名字,你可能必须做些额外的工作。假设
ScientificName
和
CommonName
都有一个
.gist
方法:
role ScientificName {
...; # all the attributes specified earlier
method gist {
join ' > ', $.kingdom, $.genus;
}
}
role CommonName {
has $.common-name is rw;
method gist { "Common name: $.common-name" }
}
class Butterfly does ScientificName does CommonName {};
.gist
方法有一个显式签名,没有标志
multi
。编译时你会得到一个错误,指出两个 ...