- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这个 perl 对象。在对象被实例化后,我试图在加载器方法中向对象添加一个新方法,然后可以稍后调用。
我已经尝试了一大堆没有用的东西。示例包括:
sub loader {
my ($self) = @_;
sub add_me {
my ($self, $rec) = @_
warn "yayyyyyy";
return $rec;
}
#here are the things I've tried that dont work:
# &{$self->{add_me}} = \&add_me;
# \&{$self->{add_me}} = \&add_me;
# assuming the class definition is in Holder::Class try to add it to symblol table
# *{Holder::Class::add_me} = \&add_me;
}
sub inject_a_sub {
my ($self, $rec) = @_;
#do stuff to $rec
return $rec;
}
do_something
的方法。我想让 eval 读取的 sub 覆盖
do_something
方法被继承,以便在从外部文件调用 sub 时运行。
最佳答案
如果您只想将功能附加到特定对象,并且不需要继承,则可以在对象中存储代码引用并调用它。
# Store the code in the object, putting it in its own
# nested hash to reduce the chance of collisions.
$obj->{__actions}{something} = sub { ... };
# Run the code
my @stuff = $obj->{__actions}{something}->(@args);
$obj->{__actions}{something}
包含代码引用。我的建议是围绕这个过程包装一个方法。
sub add_action {
my($self, $action, $code) = @_;
$self->{__actions}{$action} = $code;
return;
}
sub take_action {
my($self, $action, $args) = @_;
my $code = $self->{__actions}{$action};
return if !$code or ref $code ne 'CODE';
return $code->(@$args);
}
$obj->add_action( "something", sub { ... } );
$obj->take_action( "something", \@args );
sub Some::Class::new_method {
my $self = shift;
...
}
state
在子程序内或
my
在子程序之外。
sub inject_method {
my($object, $method_name, $code_ref) = @_;
# Get the class of the object
my $class = ref $object;
{
# We need to use symbolic references.
no strict 'refs';
# Shove the code reference into the class' symbol table.
*{$class.'::'.$method_name} = $code_ref;
}
return;
}
inject_method($obj, "new_method", sub { ... });
my $instance_class = "_SPECIAL_INSTANCE_CLASS_";
my $instance_class_increment = "AAAAAAAAAAAAAAAAAA";
sub inject_method_into_instance {
my($object, $method_name, $code_ref) = @_;
# Get the class of the object
my $old_class = ref $object;
# Get the special instance class and increment it.
# Yes, incrementing works on strings.
my $new_class = $instance_class . '::' . $instance_class_increment++;
{
# We need to use symbolic references.
no strict 'refs';
# Create its own subclass
@{$new_class.'::ISA'} = ($old_class);
# Shove the code reference into the class' symbol table.
*{$new_class.'::'.$method_name} = $code_ref;
# Rebless the object to its own subclass
bless $object, $new_class;
}
return;
}
/^${instance_class}::/
来进行这种处理。 .我把它留给你作为练习。为每个对象创建一个新类并不便宜,而且会消耗内存。
关于perl - 在 perl 中向现有对象添加新方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28373405/
如果有人能为我分解它,让我能理解它,我会非常感激。我知道它用于通过 apply 方法创建新对象。 Function.prototype.new = function () { var args
新版本的HADOOP中有一个方法。http://hadoop.apache.org/docs/current/api/org/apache/hadoop/fs/FileSystem.html#conc
根据支持库变更日志和 Fragment 类文档 ( https://developer.android.com/reference/android/support/v4/app/Fragment.ht
根据支持库更改日志和 Fragment 类文档 (https://developer.android.com/reference/android/support/v4/app/Fragment.htm
执行Async BigJob() 的无限运行任务的正确方法是什么?并且可以根据要求停止 提示:我正在尝试学习 [一种] 新方法来更新我现有的策略。 我有一个简单的程序(测试程序),它有一个开始 和停止
我将解释我的想法:我使用 python 作为谷歌应用程序引擎 + js + css 主项目将存储在 src 文件夹下,如下所示:\src \app <--- 这里是 gae 的所有 python 应用
我是一名优秀的程序员,十分优秀!