将匿名内部类重写为闭包(Java - > xtend)

时间:2013-07-04 08:07:40

标签: java closures anonymous xtend

如何将测试方法重写为xtend?我尝试了几件事,但我失败了。

用xtend编写的类

import com.google.common.base.Function

class Generator {
@Property Function<Generator, CharSequence> generateSomething;

generateFoo() {
    if( null != generateSomething) {
        generateSomething.apply(this)
    }
}

用Java编写的TestClass

@Test
public void testX() {

    Generator gen = createGenerator()

    Function<Generator, CharSequence> generateSomething= new Function<Generator, CharSequence>() {
        @Override
        public CharSequence apply( Generator input ) {
            StringConcatenation result = new StringConcatenation();
            result.append(".......");

            return result;
        }
    };

    gen.setGenerateSomething( generateSomething );

}

1 个答案:

答案 0 :(得分:0)

好的,我解决了这个问题:

import com.google.common.base.Function

class Generator {
@Property private (Generator) => CharSequence generateSomething;

generateFoo() {
    if( null != generateSomething) {
        generateSomething.apply(this)
    }
}

xtend中的重写测试方法:

@Test
def void testX() {

    Generator gen = createGenerator()

    gen.setGenerateSomething['''......''']
}