gpt4 book ai didi

java - 将 Parse Java SDK 翻译成 JRuby

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:12:24 24 4
gpt4 key购买 nike

我想看看这个 Java code会在 JRuby 中查找:

 ParseQuery query = new ParseQuery("MyClass");
query.getInBackground(myId, new GetCallback() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
objectWasRetrievedSuccessfully(object);
} else {
objectRetrievalFailed();
}
}
});

对我来说最大的困惑是匿名内部类。这是我最好的第一个猜测:

query = ParseQuery.new("GameScore")
query.getInBackground("xWMyZ4YEGZ", Class.new(GetCallback) do
def done(object, e)
# ...
end
end.new)

更新:基于此编辑:http://www.ruby-forum.com/topic/188599#823271

最佳答案

在 JRuby 中表达它的语法看似简单。 JRuby 有一个称为“闭包转换”的特性,其中传递给方法的闭包可以转换为适当的 Java 接口(interface)。来自JRuby docs :

This not only works for event listeners or Runnable, but basically for any interface. When calling a method that expects an interface, JRuby checks if a block is passed and automatically converts the block to an object implementing the interface.

因此,您的代码如下所示:

query.in_background 'xWMyZ4YEGZ' { |object, e|
# do stuff
}

'calling Java from JRuby' JRuby wiki 上的页面是解决此类问题的良好资源。

关于java - 将 Parse Java SDK 翻译成 JRuby,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11963400/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com