Java 在Android中,在某些方法之间加括号意味着什么?
我读了一些代码,发现了类似的东西,例如下面的例子Java 在Android中,在某些方法之间加括号意味着什么?,java,android,Java,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 ->
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"));
谢谢。没有。没有任何好处。
但有时可能需要。括号定义语句。所以,若您需要将代码片段视为语句,那个么应该使用括号
例如,当像这样铸造类型时
((Button)dialog.findViewById(R.id.button)).setOnClickListener(v -> Log.e(TAG, "OK"));
在这种情况下,如果您想使用按钮
类的某些特定功能,而不是从findViewById(R.id.Button)
方法返回的简单视图
类,则需要使用括号
希望有帮助。没用。但是,对于不信任或不理解语言的人来说,这通常是好的。FWIW您可以通过两种方法进行检查,或者查看生成的字节码。