我下面這樣打就是會出錯
public class Test
{
public static void main(String[] args)
{
Book book=new Book(5);
book.DeathNote();
eroBook erobook=new eroBook(50);
erobook.ERO();
}
}
class Book
{
private int pages;
public Book(int pages)
{
setPages(pages);
}
public void DeathNote()
{
System.out.print("DeathNote is on page ");printPage();
}
public void printPage()
{
System.out.println(this.pages);
}
public void setPages(int pages)
{
this.pages=pages;
}
}
class eroBook extends Book
{
public eroBook(int pages)
{ //就是這邊出問題!!
setPages(pages);
}
public void ERO()
{
System.out.print("ero page is on page ");printPage();
}
}
結果:
Test.java:34: error: constructor Book in class Book cannot be applied to given types;
{
^
required: int
found: no arguments
reason: actual and formal argument lists differ in length
實在搞不懂錯在哪邊,看書上也是這樣呼叫父類別方法的,不過我把eroBook 的建構子裡面
改用Super 去呼叫父類別就沒問題,或者直接刪除Book的建構子,不給Book初始值也可以順
利通過編譯,請問這究竟錯在哪邊呢?
拜託各位了,有這個疙瘩在這邊我根本沒辦法繼續往下讀﹍
※ 編輯: s9209122222 (122.117.207.31), 09/24/2015 17:52:34