英文:
The method inside runOnUiThread
问题
我刚开始学习Java和Android几周前,发现在尝试理解下面的代码时遇到了困难。
问题出在我不太理解setData方法是如何工作的,为什么它能够在实例初始化块之后使用。
如果有人能帮助我理解代码的这部分内容,将会很有帮助。
英文:
I just started learning Java and Android few weeks ago, and i found out having trouble while trying to understand the code down below.
runOnUiThread(new Runnable() {
Runnable setData(String _denso, String _data) {
denso = _denso;
data = _data;
return this;
}
@Override
public void run() {
}
}.setData(data.getSymbologyDenso(), data.getData())
The part im having trouble understanding is how setData method works, why is it able to be used after the Instance Initialization Block.
It would be helpful if anyone can give me a hand on understanding this part of the code.
答案1
得分: 0
你创建了一个匿名类的实例,然后调用了该实例上的 setData
方法。setData
返回了实例本身,然后将其作为参数传递给 runOnUiThread
。
无论如何,似乎 setData
仅仅是为了应付而返回 this
:我认为这不是一个好的解决方案。如果需要初始化那个 Runnable
,可能是个好主意将其创建为一个(可能是内部的)类,并提供一个适当的构造函数。
PS: 你应该遵循Java代码约定(不要在参数的首字符使用下划线)。
英文:
You're creating an instance of an anonymous class, then you're invoking the setData
method on said instance. setData
return the instance itself, which in turn gets passed as the argument of runOnUiThread
.
Anyway, it seems like setData
returns this
only to make ends meet: I don't think it is a good solution. If you need to initialize that Runnable
, it may be a good idea to make it a (possibly inner) class with a proper constructor.
PS: you should follow the Java Code Conventions (do not use an underscore a parameter's first character).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论