Perl6:子条款中的构造函数

前端之家收集整理的这篇文章主要介绍了Perl6:子条款中的构造函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有办法从子类中的构造函数分配超类中声明的实例变量?我已经习惯使用BUILD()作为构造函数,但我想知道这是不是一个好主意.即:

use v6;      

class File                                                                                                                                                                                                                                    
{                                                                                                                                                                                                                                             
    has $!filename;                                                                                                                                                                                             
}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

class XmlFile is File                                                                                                                                                                                                                         
{                                                                                                                                                                                                                                             
    submethod BUILD(:$!filename)                                                                                                                                                                                                              
    {
    }
}

my XmlFile $XF = XmlFile.new(filename => "test.xml");

上面的代码不起作用,提示错误:“属性$!filename未在类XmlFile中声明”.这是使用正确的访问者的问题吗?改变“!”至 ”.”没有解决问题.

解决方法

你在那里一半.您必须对代码进行正确的两项更改:

class File {
    has $.filename;                   # 1. Replace `!` with `.`
}

class XmlFile is File {
    submethod BUILD(:$filename) { }   # 2. Remove `!`
}

dd my XmlFile $XF = XmlFile.new(filename => "test.xml");

# XmlFile $XF = XmlFile.new(filename => "test.xml")

在File类中用$.filename替换$!filename会在该类中生成公共访问器方法(.filename).

(注意,属性在技术上总是对类是私有的,即对其他类总是不可用,甚至是trusted个.当你看到短语“public attribute”时,它实际上意味着有一个“公共访问器”来控制对相应的底层私有属性的访问.)

删除!来自XmlFile类中的BUILD签名的twigil意味着您不再尝试引用不存在的XmlFile属性,而只是传递命名参数.

Object Construction

Due to the default behavior of BUILDALL and BUILD submethods,named arguments to the constructor new derived from Mu can correspond directly to public attributes of any of the classes in the method resolution order,or to any named parameter of any BUILD submethod.

(这种“公共属性”用词不当.它的意思是“具有匹配公共访问者的属性”.)

猜你在找的Perl相关文章