gpt4 book ai didi

java - 错误消息 'Cannot be resolved or is not a field'

转载 作者:行者123 更新时间:2023-12-01 15:20:38 29 4
gpt4 key购买 nike

现在我正在研究责任链设计模式并使用 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);
}

最佳答案

这里有一个建议:研究 Sun Java 编码标准并牢记在心。在这个小代码示例中,您太频繁地破坏它们了。

Java区分大小写:“discount”与“Discount”不同; “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;
}
}
}

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

关于java - 错误消息 'Cannot be resolved or is not a field',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10972788/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com