加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl6:子条款中的构造函数

发布时间:2020-12-16 06:24:31 所属栏目:大数据 来源:网络整理
导读:有没有办法从子类中的构造函数分配超类中声明的实例变量?我已经习惯使用BUILD()作为构造函数,但我想知道这是不是一个好主意.即: use v6; class File { has $!filename; } class XmlFile is File { submethod BUILD(:$!filename) { }}my XmlFile $XF = XmlF
有没有办法从子类中的构造函数分配超类中声明的实例变量?我已经习惯使用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.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读