2012-05-11 51 views
0

我是一名學生,正在創建一個使用數組創建類以模擬組合的項目。我假設目前爲止我擁有一切,但似乎我在驅動程序類中的打印語句中遇到問題。我不確定是否我的方法將兩者聯繫在一起。任何信息都會感恩。在包含數組的組合模型中打印方法鏈接

public class MyWord 
{ 
    private String word;  
    public MyWord(){ 
    word = "Null"; 
    } 
    public MyWord(String s){ 
    word = s; 
    } 
    public String getWord(){ 
    return word; 
    } 
    public void setWord(String w){ 
    word = w; 
    } 
    public void print(){ 
    System.out.println(word); 
    } 
} 

public class Page 
{ 
private MyWord[] words = new MyWord[5]; 
private int pageNumber;  
public Page(){ 
    MyWord words[] = {} ; 
    pageNumber = 0; 
}  
public Page(MyWord[] a, int b){ 
    words = a; 
    pageNumber = b; 
}  
public MyWord[] getWord(){ 
    return words; 
} 
public int getPageNumber(){ 
    return pageNumber; 
}  
public void setMyWord(MyWord[] a){ 
    words = a; 
}  
public void setPageNumber(int b){ 
    pageNumber = b; 
}  
public void print(){ 
    System.out.print(" Page Number: " + pageNumber + " " + words); 
} 
} 

public class Book 
{ 
private Page[] p = new Page[5]; 
private String title;  
public Book(){ 
    Page[] p = {}; 
    title = " "; 
} 
    public Book(Page[] pa, String ti){ 
    p = pa; 
    title = ti; 
} 
    public Page[] getPage(){ 
    return p; 
} 
    public String getTitle(){ 
     return title; 
}  
public void setPage(Page[] x){ 
    p = x; 
} 
public void setTitle(String y){ 
    title = y; 
}  
public void print(){ 
    System.out.print("Book info:" + p + " " + title); 
} 
} 

public class Series 
{ 
private Book bookOne, bookTwo, bookThree; 
private double price; 

public Series(){ 
    bookOne = null; 
    bookTwo = null; 
    bookThree = null; 
    price = 0; 
} 

public Series(Book one, Book two, Book three, double p){ 
    bookOne = one; 
    bookTwo = two; 
    bookThree = three; 
    price = p; 
}  
public Book getBookTwo(){ 
    return bookTwo; 
}  
public Book getBookOne(){ 
    return bookOne; 
}  
public Book getBookThree(){ 
    return bookThree; 
}  
public double getPrice(){ 
    return price; 
} 
public void setBookOne(Book bookOne){ 
    this.bookOne = bookOne; 
} 
public void setBookTwo(Book bookTwo){ 
    this.bookTwo = bookTwo; 
}  
public void setBookThree(Book bookThree){ 
    this.bookThree = bookThree; 
}  
public void setPrice(double price){ 
    this.price = price; 
} 
public void print(){ 
    System.out.println("Series info"); 
    System.out.println("Book one:" + bookOne + " Book Two: " +bookTwo 
    + " Book Three: " + bookThree + "Price: " + price); 
    } 
} 

public class Driver 
{ 
public static void main(String args[]){  
    MyWord[] w1 = new MyWord[2]; 
    w1[0] = new MyWord("Hello"); 
    w1[1] = new MyWord("Hola"); 

    Page[] p = new Page[2]; 
    p[0] = new Page(w1, 20); 
    p.print(); 
    } 
} 
+2

什麼問題是什麼? –

回答

1

pPage[]類型,即, 「頁的數組」 的。並且數組沒有print()方法。所以聲明p.print()不會編譯(你應該說在你的問題中,並加入了確切的錯誤信息)。

要打印陣列的所有頁面,你需要循環陣列之上:

for (Page page : p) { 
    page.print(); 
} 

請避免單字母的變量,並使用複數形式的數組和集合:Page[] pages = new Page[2];