我在文档中找不到任何内容,但似乎子类中没有访问其超类的私有变量.我对吗?
class A {
has $!a;
}
class B is A {
has $.b;
method set_a($x) {
$!a = $x;
}
}
my $var = B.new();
$var.set_a(5);
say $var.a;
Run Code Online (Sandbox Code Playgroud)
这会给出一条错误消息:
Attribute $!a not declared in class B
Run Code Online (Sandbox Code Playgroud)
BTW在哪里阅读文档中的类?我只发现了一个相当短的部分类和对象.
有没有办法从子类中的构造函数分配超类中声明的实例变量?我已经习惯使用BUILD()作为构造函数,但我想知道这是不是一个好主意.即:
use v6;
class File
{
has $!filename;
}
class XmlFile is File
{
submethod BUILD(:$!filename)
{
}
}
my XmlFile $XF = XmlFile.new(filename => "test.xml");
Run Code Online (Sandbox Code Playgroud)
上面的代码不起作用,提示错误:"属性$!filename未在类XmlFile中声明".这是使用正确的访问者的问题吗?改变"!" 至 "." 没有解决问题.