gpt4 book ai didi

java - Android中一些方法之间加括号是什么意思?

转载 作者:行者123 更新时间:2023-11-29 22:46:56 27 4
gpt4 key购买 nike

我读了一些代码,发现了类似的东西,例如下面。

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"));

在这种情况下,如果您想使用 Button 类的某些特定功能而不是从 findViewById(R. id.button) 方法。

希望对您有所帮助。

关于java - Android中一些方法之间加括号是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58252372/

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