Perl6: Constructors in Subclasses

Is there a way to assign instance variables declared in a superclass from a constructor in a subclass? I'm used to using BUILD () as a constructor, but I wonder if this is a good idea. I.e:

use v6;      

class File                                                                                                                                                                                                                                    
{                                                                                                                                                                                                                                             
    has $!filename;                                                                                                                                                                                             
}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

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

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

The above code does not work, causing an error: "The $! Filename attribute is not declared in the XmlFile class." Is it a matter of using the correct access? Change "!" to "." does not solve the problem.

+4
source share
2 answers

You're halfway there. You must make the right two changes to your code:

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")

Replacing $!filenamewith $.filenamein a class Filegenerates a public access method ( .filename) in that class.

( : , .. , . " " " ", .)

! BUILD XmlFile , XmlFile .

Per :

- BUILDALL BUILD new, Mu, .

(, " " . " ".)

+4

, , . (, , ?) , , - .

class File                                                                                                                                                                                                                                    
{                                                                                                                                                                                                                                             
    has $!filename;                                                                                                                                                                                             
}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

class XmlFile is File                                                                                                                                                                                                                         
{   
    has $!filename;

    submethod BUILD(:$!filename)                                                                                                                                                                                                              
    {
    }
}

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

"" "", class XmlFile, class File, , .

handles .

class XmlFile
{
    has File $!file handles<some methods>;
    ...
}
0

Source: https://habr.com/ru/post/1679272/


All Articles