2016-03-29 28 views
0

我的IDE(intelliJ)告訴我它無法解析「this.x」中的變量x。我是否正確地做這件事?無法解析私有方法中的變量

private Double localTax(){ 
double x = 0; 

    if(grossIncome <= 45000){ 
     x = (grossIncome * 0.0115); 
    } 
    else if (grossIncome > 45000){ 
     x = (45000 * 0.0115); 
    } 
    return this.x; 
} 

回答

3

編號x是您的方法的局部變量。 this引用實例字段,而不是本地變量。刪除this以獲得您的代碼編譯。

+0

非常感謝! – Spectre

0

您正在設置局部變量x。

this.x引用一個實例變量。

public class hello { 
    private int x; //this is this.x 

    public int foo(int globalIncome){ 
    int x = globalIncome; 
    return x; //returns globalIncome 
    } 
} 
+0

你爲什麼要用this.x作爲靜態變量? –

+0

也this.x不引用全局變量。它指的是實例變量。 –