任何人都可以帮我把这个java片段“翻译”成perl / Moose术语吗?尝试理解java对象语法/逻辑,我只知道perl。
根据评论进行编辑:该片段来自xwiki包 - http://platform.xwiki.org/xwiki/bin/view/DevGuide/WritingComponents ... 它对于分析来说太大了,那么剩下的代码呢?是可能的(仅用于解释 - 忽略@lines?'@ something'的一般含义是什么?
@Component("hello")
public class HelloWorldScriptService implements ScriptService
{
@Requirement
private HelloWorld helloWorld;
public String greet()
{
return this.helloWorld.sayHello();
}
}
寻找类似下一个perl片段的东西,但不知道“@Component,@ Request - etc :(
package HelloWorldScriptService;
use Moose;
sub greet {
return $self->
}
存在一些用perl-ish术语解释java的文档? (至少,一些基础知识)
答案 0 :(得分:5)
不确定@Component,但还有更多:
package HelloWorld;
use Moose;
sub say_hello {
print "Hello";
}
1;
package HelloWorldScriptService;
use Moose;
has 'hello_world' => ( is => 'rw', isa => 'HelloWorld' );
# TODO - will need to instantiate the hello_world object somewhere...
sub greet {
my ($self) = @_;
return $self->hello_world->say_hello();
}
1;
因为helloWorld是原始属性中的私有属性,所以你可能希望在名称'hello_world'前面添加下划线(并添加init_args => undef) - 这不会像Java一样强制执行隐私,但会向任何寻找的人显示在它的概念上是私有的代码(并阻止设置new())
答案 1 :(得分:3)
这些注释是某些Dependency Injection框架的元数据(看起来XWiki使用自己的依赖注入支持,并且它使用了这些注释)。
当这个类单独使用时,它们没有任何特殊含义。但是当它在DI容器中使用时,@Requirement
表示容器注入helloWorld
的值,@Component
表示可以将类本身注入其他组件。