2015-02-07 66 views
0

在調用doReading()之後,頁面的值應該改變,但不改變。沒有聲明int頁面就可以做到這一點;在MyMath2和MyScience2類中。輸出應該是這樣的: 閱讀之前: MATH-必須閱讀 5頁讀數後: MATH-必須閱讀調用方法後Int值不會改變

3頁

摘要Homework2

public abstract class Homework2 implements Processing 
{ 
    private int pagesToRead; 
    private String typeHomework; 

{ 
    // initialise instance variables 
    pagesToRead = 0; 
    typeHomework = "none"; 
} 

public Homework2(int pages, String hw) 
{ 
    pagesToRead = pages; 
    typeHomework = hw; 
} 

public abstract void createAssignment(int pages); 

public int getPages() 
{ 
    return pagesToRead; 
} 

public void setPagesToRead(int p) 
{ 
    pagesToRead = p; 
} 

public String getTypeHomework() 
{ 
    return typeHomework; 
} 

public void setTypeHomework(String hw) 
{ 
    typeHomework = hw; 
} 

public String toString() 
{ 
    return "reading: \n" + typeHomework + " - must read " + pagesToRead + " pages"; 
} 

} 

TestHomework2

public class TestHomework2 
{ 

public static void main(String []args) 
{ 

    List<Homework2> tester = new ArrayList<Homework2>(); 

tester.add(new MyMath2(5, "Math"));     
tester.add(new MyScience2(7, "Science")); 


for (Homework2 c: tester) 
    { 
    System.out.print("Before "); 
    System.out.println(c); 
    c.doReading(); 
    System.out.print("After "); 
    System.out.println(c); 
    } 


} 
} 

接口過程唱

public interface Processing 
{ 
void doReading() 
} 

MyMath2

public class MyMath2 extends Homework2 implements Processing 
{ 
    int pages; 

public MyMath2(int pages, String hw) 
{ 
    super(pages,hw); 
} 

public void createAssignment(int pages) 
{ 
    setTypeHomework("Math"); 
    setPagesToRead(pages); 
} 

public void doReading() 
{ 
    pages = pages - 2; 
} 
} 

MyScience2

public class MyScience2 extends Homework2 implements Processing 
{ 
    int pages; 

public MyScience2(int pages, String hw) 
{ 
    super(pages,hw); 
} 

public void createAssignment(int pages) 
{ 
    setTypeHomework("Science"); 
    setPagesToRead(pages); 
} 

public void doReading() 
{ 
    pages = pages - 3; 
} 
} 
+1

你永遠不會改變變量pagesToRead,是嗎? – Anil 2015-02-07 17:45:09

回答

1

田不覆蓋。這意味着當調用doReading()時,初始pagesToRead不會更新,因爲該方法更改了pages

你應該只是做:Homework2

public String toString() 
{ 
    return "reading: \n" + typeHomework + " - must read " + pagesToRead + "  pages"; 
} 

存在的

public void doReading(){ 
    setPagesToRead(getPages() - 2); 
} 
1
​​

類的toString()返回的pagesToRead。但在doReading您減少頁面不pagesToRead:

public void doReading() 
{ 
    pages = pages - 2; 
} 

減少pagesToRead:

public void doReading() 
{ 
    pagesToRead = pagesToRead - 2; 
} 
+0

謝謝,現在我看到了我的錯誤 – apsstudent 2015-02-07 17:55:33

1

當通過調用doReading改變頁數,你改變MyMath2對象的頁面的價值(財產MyMath2的目的)。您打印的號碼來自Homework2.pagesToRead字段。

當您調用doReading方法並且因此獲得相同的值時,Homework2中的此字段永遠不會更改。

public void doReading(){ 
    pages = pages - 2; 
    super.setPagesToRead(pages); 
} 

或者你完全MyMath2刪除頁面字段並使用字段從父對象一樣:

public void doReading(){ 
    super.setPagesToRead(super.getPagesToRead() - 2); 
} 

同樣適用於MyScience2您可以通過設置您在MyMath2對象等設定值修正這個類也是如此。

相關問題