Perl6:子条款中的构造函数

Perl6:子条款中的构造函数,第1张

概述有没有办法从子类中的构造函数分配超类中声明的实例变量?我已经习惯使用BUILD()作为构造函数,但我想知道这是不是一个好主意.即: use v6; class File 有没有办法从子类中的构造函数分配超类中声明的实例变量?我已经习惯使用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.

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

总结

以上是内存溢出为你收集整理的Perl6:子条款中的构造函数全部内容,希望文章能够帮你解决Perl6:子条款中的构造函数所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1253093.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存