简体   繁体   English

如何在不调用call方法的情况下调用functor对象的函数

[英]How to call functor objects's function without invoking call method

I have a functor object: 我有一个仿函数对象:

private static Func1<MyEvent, Observable<Data>> getDataOnEvent = new Func1<MyEvent, Observable<Data>>() {
    @Override
    public Observable<Data> call(MyEvent event) {
        return ApiFactory.get().getData()
    }
};

For its invoking I need to do this: 对于其调用,我需要这样做:

result = getDataOnEvent.call(someEvent)

Is it possible to do this instead: 是否可以这样做呢?

result = getDataOnEvent(someEvent)

Like it is done with Python and Javascript? 像是用Python和Javascript完成的吗? Maybe a new version of java OR some library like Lombok? 也许是Java的新版本或Lombok之类的库?

Just use, 随便用

private static Observable<Data> getDataOnEvent(MyEvent event) {
    return ApiFactory.get().getData()
}

and you can call result = getDataOnEvent(someEvent); 您可以调用result = getDataOnEvent(someEvent); whenever you need it. 随时随地。 As you can see, writing it this way, will save even more boiler code than the five letters .call on the invocation side. 如您所见,以这种方式编写代码,将比调用侧的五个字母.call节省更多的锅炉代码。

If Func1 is a functional interface, you can use ContainingClass::getDataOnEvent wherever a Func1<MyEvent, Observable<Data>> is expected. 如果Func1是功能性接口,则可以在需要Func1<MyEvent, Observable<Data>>任何地方使用ContainingClass::getDataOnEvent You can also store it into a static variable, if you prefer using the simple identifier getDataOnEvent as function: 如果您更喜欢使用简单标识符getDataOnEvent作为函数,也可以将其存储到static变量中:

private static Func1<MyEvent, Observable<Data>> getDataOnEvent
                                              = ContainingClass::getDataOnEvent;

Then you can use getDataOnEvent(event) to call it or getDataOnEvent to refer to it as a Func1 instance whenever you need it. 然后你可以使用getDataOnEvent(event)调用或getDataOnEvent指它作为一个Func1例如当您需要它。

If Func1 is not a functional interface, then you can't create the function in this compact form, but on the other hand, in that case it wouldn't be reasonable to ask for a support for calling an arbitrary method without naming it explicitly, either. 如果Func1不是函数接口,则不能以这种紧凑的形式创建函数,但另一方面,在这种情况下,要求对调用任意方法的支持而不显式命名是不合理的,或者。

I know what you mean, groovy code call a closure like this: 我知道您的意思,普通代码调用这样的闭包

def code = { 123 };

//can be called like any other method if the variable is a closure.
assert code() == 123;
//can be call explicitly by using `call` method
assert code.call() == 123;

javascript code call a function like this: javascript代码调用如下函数

let code = () => 123;

//can be called like any other function if the variable is a function.
assert code() == 123;
//can be call explicitly by using `call` method
assert code.call() == 123;

But I can tell you java gammar not support this feature for fields/variables, maybe in the next jdk will be enable this feature which a field/variable refer to Callable . 但是我可以告诉您Java gammar不支持字段/变量的此功能,也许在下一个jdk中将启用此功能,而字段/变量指的是Callable

Fortunately, Single-Static-Import Declarations supports calling a method directly if the static member is a method, for example: 幸运的是,如果静态成员是方法,则单一静态导入声明支持直接调用方法,例如:

import static java.lang.Math.abs;

assert abs(-1) == 1;

If you really want to make an identifier called like a method call, you can fake something like this: 如果您真的想使标识符称为方法调用,则可以伪造如下内容:

class ApiFactory {

    public static Func1<MyEvent, Observable<Data>> getDataOnEvent = new Func1<MyEvent, Observable<Data>>() {
        public Observable<Data> call(MyEvent event) {
            return ApiFactory.get().getData();
        }
    };

    public static Observable<Data> getDataOnEvent(MyEvent event) {
        return getDataOnEvent.call(event);
    }

}

then you can call like this: 那么您可以像这样致电:

import static ${package}.ApiFactory.getDataOnEvent;
// which is calling a static method
result = getDataOnEvent(event);
// which is calling a static field
result = getDataOnEvent.call(event);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM