2011-06-17 66 views
3

任何人都可以幫助我把這個Java片段翻譯成perl/Moose術語嗎?嘗試瞭解java對象語法/邏輯,我只知道perl。什麼是此Java片段的Perl等效物? (在Perl中的Java解釋)

編輯根據評論:該片段來自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-> 
} 

存在着一些文件,其中Java是用Perl上下的角度來解釋(至少,一些基礎知識)

+1

Annotations @Component和@Requirement不是java庫的一部分。爲了轉碼,你必須看看他們應該做什麼。如果它有幫助,它們看起來像Spring註釋。 – charisis 2011-06-17 11:25:29

+0

@Component可能是spring,但@Requirement不是。同意你需要告訴我們這些註釋來自哪裏! – stevevls 2011-06-17 11:27:22

回答

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是原始的私有屬性,所以您可能希望在名稱前添加下劃線(並添加init_args => undef) - 這不會像Java一樣強制保護隱私,但會向任何顯示在它的概念私有代碼(並防止設置新的())

3

這些註釋是一些Dependency Injection框架(它看起來像XWiki實現使用其自身的依賴注入的支持,這些註釋都是由它使用)的元數據。

當這個類是單獨使用,它們沒有任何特殊含義。但是當它在DI容器中使用時,@Requirement指示helloWorld的值將由容器注入,並且@Component指示該類本身可以注入到其他組件中。