我想用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;' –