2012-06-10 30 views
0

現在我正在研究責任鏈設計模式,並使用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

以下是一個建議:研究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; 
     } 
    } 
} 

我覺得這個代碼是不是有點混亂了。難怪你有問題。

+0

是的,我知道。我用Notepad ++的替換函數做了一些修改,所以不符合標準。 – Raphael

+0

不好.......我根本不在乎你的設計。我以爲你在使用Eclipse? – duffymo

+1

@Raphael:那麼請再次修改它,使其*爲*標準。特別是如果你要求志願者嘗試閱讀和理解你的代碼。 1+ –

相關問題