这个 Java 代码片段的 Perl 等价物是什么? (Perl 术语的 Java 解释)

发布于 2024-11-15 22:21:49 字数 798 浏览 3 评论 0原文

任何人都可以帮我将这个 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,@Requirement - 等:(

package HelloWorldScriptService;
use Moose;
sub greet {
    return $self->
}

存在一些文档用 perl 式的术语解释了 java(至少是一些基础知识)

Can anybody help me "translate" this java snippet into perl/Moose terms?. Trying understand java object-syntax/logic and I know only perl.

EDIT based on comments: The snippet is coming form the xwiki package - http://platform.xwiki.org/xwiki/bin/view/DevGuide/WritingComponents...
It is too big for analyze, so what about the remaining of the code? Is possible (for the explanation only - ignore the @lines? What is the general meaning of the '@something'?

@Component("hello")
public class HelloWorldScriptService implements ScriptService
{
    @Requirement
    private HelloWorld helloWorld;

    public String greet()
    {
        return this.helloWorld.sayHello();
    }
}

Looking for something like the next perl fragment, but have no idea about "@Component, @Requirement - etc :(

package HelloWorldScriptService;
use Moose;
sub greet {
    return $self->
}

Exists some docs where java is explained with perl-ish terms? (at least, some basics)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静水深流 2024-11-22 22:21:49

不确定 @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() 上进行设置)

not sure about the @Component, but here's some more:

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;

since helloWorld is a private attribute in the original, you might want to prepend the name 'hello_world' with an underscore (and add init_args => undef) - this doesn't enforce privacy like Java but does show to anyone looking at the code that it's conceptually private (and prevents setting on new())

溺深海 2024-11-22 22:21:49

这些注释是某些 依赖注入 框架的元数据(看起来 XWiki 使用自己的依赖注入支持,并且这些注释被它使用)。

当此类单独使用时,它们没有任何特殊含义。但是当在 DI 容器中使用时,@Requirement 表示 helloWorld 的值要由容器注入,@Component 表示类本身可以注入到其他组件中。

These annotations are metadata for some Dependency Injection framework (it looks like XWiki uses its own dependency injection support, and these annotations are used by it).

When this class is used on its own, they don't have any particular meaning. But when it's used inside a DI container, @Requirement indicates that the value of helloWorld is to be injected by the container, and @Component indicates that the class itself can be injected into other components.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文