相关疑难解决方法(0)

在Perl 6中继承私有属性

我在文档中找不到任何内容,但似乎子类中没有访问其超类的私有变量.我对吗?

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在哪里阅读文档中的类?我只发现了一个相当短的部分类和对象.

oop perl6

8
推荐指数
1
解决办法
187
查看次数

Perl6:子条款中的构造函数

有没有办法从子类中的构造函数分配超类中声明的实例变量?我已经习惯使用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中声明".这是使用正确的访问者的问题吗?改变"!" 至 "." 没有解决问题.

constructor perl6

5
推荐指数
1
解决办法
134
查看次数

标签 统计

perl6 ×2

constructor ×1

oop ×1