在Android中,某些方法之间放置括号是什么意思?

我阅读了一些代码并发现了类似的内容,例如见下文。


Dialog dialog = new Dialog(this);

dialog.setContentView(R.layout.custom_dialog);

dialog.show();


// What is the purpose of parentheses here? Is there any benefit?

(dialog.findViewById(R.id.button)).setOnClickListener(v -> Log.e(TAG, "OK"));


// instead of

dialog.findViewById(R.id.button).setOnClickListener(v -> Log.e(TAG, "OK"));

谢谢。


慕丝7291255
浏览 99回答 1
1回答

catspeake

没有。一点好处都没有。但有时可能需要它。括号定义语句。因此,如果您需要将代码片段视为语句 - 您应该使用括号。例如,当像这样铸造类型时((Button)dialog.findViewById(R.id.button)).setOnClickListener(v -> Log.e(TAG, "OK"));Button在这种情况下,如果您想使用类的某些特定功能而不是View从方法返回的简单类,则需要括号findViewById(R.id.button)。希望能帮助到你。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java