Error message 'Cannot be resolved or is not a field'

╄→гoц情女王★ 提交于 2019-12-25 08:57:02

问题


Right now I'm studying the chain of responsibility design pattern and am using Eclipse.

And I'm trying to compile this code, but I have the compiling error "isLast cannot be resolved or is not a field":

public class OverFive implements Discount {
    private Discount next; //
    public boolean isLast = false;

    public void setNext(Discount next, boolean Last) {
        this.next = next;
        this.next.isLast = Last; // Here is the error.
    }

    public double DoDiscount(Budget budget) {
        if (budget.getValue() > 500) {
            return budget.getValue() * 0.10;
        }
        if (this.next.isLast == false) {
            return next.DoDiscount(budget);
        }
        else {
            return 0;
        }
    }
}

And now, here is the interface:

public interface Discount {

    double DoDiscount(Orcamento orcamento);
        void setNext(Discount next, boolean Last);
    }

回答1:


Here's one recommendation: study the Sun Java coding standards and take them to heart. You're breaking them too frequently in this small code sample.

Java is case sensitive: "discount" is not the same as "Discount"; "dodiscount" is not the same as "DoDiscount".

public interface Discount {

    double doDiscount(Orcamento orcamento);
    void setNext(Desconto next, boolean last);
    void setLast(boolean last);
} 

And the implementation:

public class OverFive implements Discount {
    private Desconto next;
    private boolean last = false;

    public void setLast(boolean last) {
        this.last = last;
    }

    public void setNext(Desconto next, boolean last) {
        this.next = next;
        this.setLast(last);
      }

    // this method is utter rubbish.  it won't compile.
    public double doDiscount(Orcamento budget){
        if (budget.getValue() > 500){
            return budget.getValue() * 0.10;
        }if (this.next.isLast == false){
            return next.discount(budget);
        }else{
            return 0;
        }   
    }
}

I think this code is more than a little confusing. No wonder you're having problems.




回答2:


I'm not sure if this is the same type of issue as the above, but I had the same error. In my case , I was using an older version of Eclipse that apparently didn't like having a class with same name as the package. I fixed the issue by giving the package a different name.



来源:https://stackoverflow.com/questions/10972788/error-message-cannot-be-resolved-or-is-not-a-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!