我的perl 6代码中的基类中有一个过滤方法。我在派生类中重写了过滤器。跟随之类的东西。
3 class list_filter {
4 has @.my_list = (1..20);
5
6 # will be overriding this in derived classes
7 method filter($l) { return True; }
8
9 # same print method I will be calling from all derived class objects
10 method print_filtered_list() {
11 my @outlist = self.get_filtered_list(&{self.filter});
12 say @outlist;
13 }
14
15 # private
16 method get_filtered_list(&filter_method) {
17 my @newlist = ();
18 for @.my_list -> $l {
19 if (&filter_method($l)) { push(@newlist, $l); }
20 }
21 return @newlist;
22 }
23 }
24
25 class list_filter_lt_10 is list_filter {
26 method filter($l) {
27 if ($l > 10) { return False; }
28 return True;
29 }
30 }
31
32 class list_filter_gt_10 is list_filter {
33 method filter($l) {
34 if ($l < 10) { return False; }
35 return True;
36 }
37 }
38
39 my $listobj1 = list_filter_lt_10.new();
40 $listobj1.print_filtered_list(); # expecting 1..10 here
41
42 my $listobj2 = list_filter_gt_10.new();
43 $listobj2.print_filtered_list(); # expecting 11..20 here
希望有不同的派生类对象,它们只是覆盖过滤器方法,并使用相同的基类功能打印出已过滤的列表。
但是收到如下错误。
Too many positionals passed; expected 0 or 1 arguments but got 2
in method print_filtered_list at ./b.pl6 line 11
in block <unit> at ./b.pl6 line 40
我该如何解决这个问题?我想将过滤方法作为另一个方法的参数传递。