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

perl – 我可以使用MooseX :: Declare定义类外的函数吗?

发布时间:2020-12-15 21:52:18 所属栏目:大数据 来源:网络整理
导读:我最近开始使用模块 MooseX::Declare.我喜欢它的语法.它优雅而整洁.有没有人遇到过你想要在类中编写许多函数(其中一些很大)和运行到页面中的类定义的情况?是否有任何解决方法使类定义只是声明函数和类外的实际函数定义? 我要找的是这样的 – class BankAcc
我最近开始使用模块 MooseX::Declare.我喜欢它的语法.它优雅而整洁.有没有人遇到过你想要在类中编写许多函数(其中一些很大)和运行到页面中的类定义的情况?是否有任何解决方法使类定义只是声明函数和类外的实际函数定义?

我要找的是这样的 –

class BankAccount {
    has 'balance' => ( isa => 'Num',is => 'rw',default => 0 );
    # Functions Declaration.
    method deposit(Num $amount);
    method withdraw(Num $amount);
}

# Function Definition.
method BankAccount::deposit (Num $amount) {
    $self->balance( $self->balance + $amount );
}

method BankAccount::withdraw (Num $amount) {
    my $current_balance = $self->balance();
    ( $current_balance >= $amount )
    || confess "Account overdrawn";
    $self->balance( $current_balance - $amount );
}

我可以看到有一种方法可以使类变异.有谁知道怎么做?

解决方法

容易(但需要添加到文档).
class BankAccount is mutable {
}

顺便说一句,为什么要在课外定义你的方法?

你可以去

class BankAccount is mutable {
    method foo (Int $bar) {
         # do stuff
    }
}

(编辑:李大同)

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

    推荐文章
      热点阅读