我想用Perl 6编写一些类来测试Perl 6类和方法。构造函数指向perl6中的类
下面是代码:
class human1 {
method fn1() {
print "#from human1.fn1\n";
}
}
class human2 {
method fn1() {
print "#from human2.fn1\n";
}
}
my $a = human1.new();
my $b = human2.new();
$a.fn1();
$b.fn1();
print "now trying more complex stuff\n";
my $hum1_const = &human1.new;
my $hum2_const = &human2.new;
my $c = $hum2_const();
$c.fn1();
基本上我希望能够为使用的human1
构造函数或human2
构造,能够动态地建立$c
对象。但我发现了以下错误:
Error while compiling /usr/bhaskars/code/perl/./a.pl6
Illegally post-declared types:
human1 used at line 23
human2 used at line 24
如何创建$c
使用函数指针来选择我使用的构造函数?
尝试在声明'$ hum2_const'时删除&符:'my $ hum2_const = human2。new;'在'$ hum2_const'前加一个&符号,并在定义'$ c'时删除括号:'my $ c =&$ hum2_const;' –