Java - 静态变量和具有相同名称的参数 [英] Java - static variable and parameter with same name
本文介绍了Java - 静态变量和具有相同名称的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个Helper类,如下所示:
Suppose I have a Helper class as below:
public class Helper {
private Context context;
private static HelperListener listener;
public Helper(Context context, HelperListener listener) {
this.context = context;
listener = listener; // Can't tell which one
}
}
context
和 listener
是在构造函数中只设置一次的变量。
context
and listener
are variables that will be set only once, in the constructor.
context
不是 static
,因此我可以使用 this.context
。
context
is not static
, hence I can differentiate the variable from the parameter using this.context
.
监听器
,另一方面,静态
。当谈到静态变量时,有什么方法可以将它与参数区分开来吗?
listener
, on the other hand, is static
. Is there any way to differentiate it from the parameter when it comes to static variables?
推荐答案
你可以使用 Helper.listener = listener;
虽然不建议从构造函数设置静态变量的值。
You could use Helper.listener = listener;
although setting the value of a static variable from a constructor is not recommended.
这篇关于Java - 静态变量和具有相同名称的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文