加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

这两个语句在perl中是否总是相同的?

发布时间:2020-12-15 21:50:57 所属栏目:大数据 来源:网络整理
导读:$obj-SUPER::promote();$obj-SUPER-promote(); 谁知道他们是否一样? 解决方法 不. – operator表示调用引用(在本例中为对象引用),它将查找SUPER方法,而不是超级基类. 这是显示它的代码: #!/usr/bin/perl -wpackage MyOBJ;use strict;use warnings;use Data
$obj->SUPER::promote();

$obj->SUPER->promote();

谁知道他们是否一样?

解决方法

不. – > operator表示调用引用(在本例中为对象引用),它将查找SUPER方法,而不是超级基类.

这是显示它的代码:

#!/usr/bin/perl -w

package MyOBJ;

use strict;
use warnings;

use Data::Dumper;

sub new {
    my ($class) = @_;

    my $self = {};

    bless $self,$class;

    return $self;
}

sub promote {
    my ($self) = @_;

    print Dumper($self);

}

1;

package MyOBJ::Sub;

use strict;
use warnings;

use base 'MyOBJ';

1;

use strict;
use warnings;

my $obj = MyOBJ::Sub->new();

$obj->SUPER::promote();

运行它,你会得到:

$VAR1 = bless( {},'MyOBJ::Sub' );

当您更改要使用的最后一行时 – >而不是::你得到:

Can't locate object method "SUPER" via package "MyOBJ" at test.pl line 45.

来自“perldoc perlop”手册

The Arrow Operator

If the right side is either a “[…]”,“{…}”,or a “(…)” subscript,then the left side must be either a hard or symbolic reference to an array,a hash,or a subroutine respectively.

Otherwise,the right side is a method name or a simple scalar variable containing either the method name or a subroutine reference,and the left side must be either an object (a blessed reference) or a class name (that is,a package name)

由于左侧既不是对象引用也不是类名(SUPER是多态性的语言定义的裸字),因此它被视为一种方法,它不存在,因此也就是错误.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读