相关疑难解决方法(0)

什么是Java类中使用的变量阴影?

我正在阅读我的Deitel,Java How to Program一书,并且遇到了阴影这个词.如果允许阴影,Java类中的情况或用途是什么?

例:

public class Foo {

    int x = 5;

    public void useField() {
        System.out.println(this.x);
    }
    public void useLocal() {
        int x = 10;
        System.out.println(x);
    }
}
Run Code Online (Sandbox Code Playgroud)

java shadowing

23
推荐指数
4
解决办法
3万
查看次数

关于java中变量范围和阴影的问题

我得到了这种情况我无法理解阴影.例如以下代码

class Foo {
   int a = 5;
   void goFoo(int a) { 
       // No problem naming parameter as same as instance variable
       for (int a = 0; a < 5; a++) { }
       //Now the compiler complains about the variable a on the for loop
       // i thought that the loop block had its own scope so i could shadow
       // the parameter, why the compiler didnt throw an error when i named
       // the parameter same as the instance …
Run Code Online (Sandbox Code Playgroud)

java variables scope shadowing

4
推荐指数
1
解决办法
3637
查看次数

标签 统计

java ×2

shadowing ×2

scope ×1

variables ×1