Dan*_*ado 9 inheritance class perl6
假设一个人有A :: Very :: Shallow :: ClassA和A :: Very :: Deep :: ClassB
文件:./A/Very/Shallow/ClassA.pm6
class A::Very::Shallow::ClassA{
has Str $.label is rw;
has Str $.icon is rw;
has @.items is rw;
}
Run Code Online (Sandbox Code Playgroud)
我想继承ClassB中的ClassA,所以我写道:
文件:./A/Very/Deep/ClassB.pm6
class A::Very::Deep::ClassB is A::Very::Shallow::ClassA{
...
}
Run Code Online (Sandbox Code Playgroud)
然而,错误与:
Cannot resolve caller trait_mod:<is>(A::Very::Deep::ClassB, A::Very::Shallow::ClassA, Hash); none of these signatures match:
(Mu:U $child, Mu:U $parent)
(Mu:U $child, :$DEPRECATED!)
(Mu:U $type, :$rw!)
(Mu:U $type, :$nativesize!)
(Mu:U $type, :$ctype!)
(Mu:U $type, :$unsigned!)
(Mu:U $type, :$hidden!)
(Mu:U $type, Mu :$array_type!)
(Mu:U $type, *%fail)
(Attribute:D $attr, |c is raw)
(Attribute:D $attr, :$rw!)
(Attribute:D $attr, :$readonly!)
(Attribute $attr, :$required!)
(Attribute $attr, Mu :$default!)
(Attribute:D $attr, :$box_target!)
...
Run Code Online (Sandbox Code Playgroud)
我发现了大量关于继承的文档和示例,但似乎没有任何内容可以涵盖我认为简单的基本问题.我知道答案可能很明显,但我现在想念它.
当类与包在同一个包中时会发生什么
A::Money::Card is A::Money::Item
Run Code Online (Sandbox Code Playgroud)
我现在有点困惑所以任何指针都会很棒.先感谢您.
{在课程开始之前,这是一个缺失的空间.将其更改为:
class A::Very::Deep::ClassB is A::Very::Shallow::ClassA {
}
Run Code Online (Sandbox Code Playgroud)
应该工作得很好(如果有use声明A::Very::Shallow::ClassA).
当然,知道为什么这个空间很重要也很有趣.
该is语法仅仅是一个案件中,更普遍特征语法.使用traits,允许传递额外的参数以及特征名称.最常见的形式可能是在使用导出标签时:is export(:util).但是,也可以传递一个array(is foo[1, 2, 3]),quote words(is bar<x y z>)或hash(is baz{ a => 1, b => 2 }).(旁白:实际上,这是在colonpair语法中工作的一组相同的东西,因此:foo([1, 2, 3])可以改为编写一个命名参数:foo[1, 2, 3]);
因此,is Something{}传递你希望的类体作为特征的空Hash参数.特征只是一个多调度sub,并且没有匹配的候选者,这解释Hash了错误和调度失败的提及.
| 归档时间: |
|
| 查看次数: |
102 次 |
| 最近记录: |