Java 类中使用的变量阴影是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1092099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is variable shadowing used for in a Java class?
提问by Jayson
I'm reading my Deitel, Java How to Program book and came across the term shadowing. If shadowing is allowed, what situation or what purpose is there for it in a Java class?
我正在阅读我的 Deitel,Java How to Program 一书并遇到了shadowing一词。如果允许阴影,Java 类中有什么情况或目的是什么?
Example:
例子:
public class Foo {
int x = 5;
public void useField() {
System.out.println(this.x);
}
public void useLocal() {
int x = 10;
System.out.println(x);
}
}
采纳答案by Yishai
The basic purpose of shadowing is to decouple the local code from the surrounding class. If it wasn't available, then consider the following case.
阴影的基本目的是将本地代码与周围的类解耦。如果它不可用,请考虑以下情况。
A Class Foo in an API is released. In your code you subclass it, and in your subclass use a variable called bar. Then Foo releases an update and adds a protected variable called Bar to its class.
发布了 API 中的 Foo 类。在您的代码中,您将其子类化,并在您的子类中使用名为 bar 的变量。然后 Foo 发布更新并向其类添加一个名为 Bar 的受保护变量。
Now your class won't run because of a conflict you could not anticipate.
现在,由于您无法预料的冲突,您的课程将无法运行。
However, don't do this on purpose. Only let this happen when you really don't care about what is happening outside the scope.
但是,不要故意这样做。只有当您真的不关心范围之外发生的事情时,才允许这种情况发生。
回答by Robert Munteanu
One major purpose is to confuse people. It's bad practice and should be avoided.
一个主要目的是混淆人们。这是不好的做法,应该避免。
回答by Tom Jefferys
It can be useful for setters where you don't want to have to create a separate variable name just for the method parameter eg:
对于不想只为方法参数创建单独变量名称的 setter,它可能很有用,例如:
public void setX(int x) {
this.x = x;
}
Apart from that I'd avoid them.
除此之外,我会避免它们。
回答by z -
Shadowing is not really a java only term. In any instance where a variable declared in a scope has the same name as one in a bigger scope, that variable is shadowed.
阴影并不是真正的 Java 专有术语。在任何在作用域中声明的变量与更大作用域中的变量同名的情况下,该变量都会被隐藏。
Some common uses for shadowing is when you have inner and outer classes and want to maintain a variable with the same name.
阴影的一些常见用途是当您有内部类和外部类并希望维护同名变量时。
If you can avoid it though, you should since it may cause confusion.
如果你可以避免它,你应该,因为它可能会引起混乱。
回答by Tom Hawtin - tackline
The two common uses are constructors and set methods:
两种常见的用途是构造函数和 set 方法:
public Foo(int x) {
this.x = x;
}
public void setX(int x) {
this.x = x;
}
Very occassionally it's useful if you want a copy of the variable at a single instant, but the variable may change within the method call.
偶尔,如果您想在一个瞬间获得变量的副本,但变量可能会在方法调用中发生变化,这很有用。
private void fire() {
Listener[] listeners = this.listeners;
int num = listeners.length;
for (int ct=0; ct<num; ++ct) {
listeners[ct].stateChanged();
}
}
(Of course, a contrived example made unnecessary with the posh for loop.)
(当然,一个人为的例子对于华丽的 for 循环来说是不必要的。)