错误消息“无法解析或不是字段” [英] Error message 'Cannot be resolved or is not a field'

查看:957
本文介绍了错误消息“无法解析或不是字段”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

现在,这里是界面:

public interface Discount {

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


推荐答案

这里有一个建议: Sun的Java编码标准,并把它们带到心上。您在这个小代码示例中经常打破他们。

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区分大小写:折扣与折扣不一样; 十二进制与DoDiscount不一样。

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

执行:

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.

这篇关于错误消息“无法解析或不是字段”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆