错误消息'无法解析或不是字段'

时间:2012-06-10 22:21:35

标签: java eclipse interface

现在我正在研究责任链设计模式并使用Eclipse

我正在尝试编译此代码,但我有编译错误“isLast无法解析或不是字段”:

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;
        }
    }
}

现在,这是界面:

public interface Discount {

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

2 个答案:

答案 0 :(得分:1)

以下是一条建议:研究Sun Java编码标准并将其铭记于心。在这个小代码示例中,你经常破坏它们。

Java区分大小写:“折扣”与“折扣”不同; “dodiscount”与“DoDiscount”不同。

public interface Discount {

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

实施:

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;
        }   
    }
}

我认为这段代码不仅有点令人困惑。难怪你遇到了问题。

答案 1 :(得分:0)

我不确定这是否是与上述问题相同的类型,但我有相同的错误。就我而言,我使用的是旧版本的Eclipse,显然不喜欢使用与包名称相同的类。我通过给包裹改一个不同的名字来解决了这个问题。