我要访问Book类内部的Page类的数据。(例如t_export_details
)
我确实喜欢b.pages.getPageNum()
但这是行不通的。我无法访问Page类方法。
如何访问?
谢谢。
b.pages = p;
public class Book<Page> {
private String name;
ArrayList<Page> pages = new ArrayList<Page>();
public Book(String name) {
this.name = name;
}
public String getBook() {
return name;
}
}
public class Main {
public static void main(String[] args) {
ArrayList<Page> p = new ArrayList<>();
Page p1 = new Page(22);
Page p2 = new Page(33);
Page p3 = new Page(44);
Page p4 = new Page(55);
p.add(p1);
p.add(p2);
p.add(p3);
p.add(p4);
Book b = new Book("harrypotter");
b.pages = p;
}
}
答案 0 :(得分:0)
创建公共获取者的二传手
public class Book {
private String name;
ArrayList<Page> pages = new ArrayList<Page>();
public Book(String name) {
this.name = name;
}
public String getBook() {
return name;
}
public List<Page> getPages() {
return this.pages;
}
public void setPages(List<Page> pages) {
this.pages = pages;
}
}
然后设置
b.setPages(p);
或访问页面
b.getPages().get(0).getPageNum();
答案 1 :(得分:0)
public class Book<Page> implements Iterable<Page> {
private String name;
ArrayList<Page> pages = new ArrayList<>();
public Book(String name) {
this.name = name;
}
public String getBook() {
return name;
}
public ArrayList<Page> getPages() {
return this.pages;
}
public void setPages(ArrayList<Page> pages) {
this.pages = pages;
}
@Override
public Iterator<Page> iterator() {
BookIterator bookIterator = new BookIterator<Page>();
bookIterator.pages = this.pages;
return bookIterator;
}
}
public class Main {
public static void main(String[] args) {
ArrayList<Page> p = new ArrayList<>();
Page p1 = new Page(22, true);
Page p2 = new Page(33, false);
Page p3 = new Page(44, true);
Page p4 = new Page(55, false);
p.add(p1);
p.add(p2);
p.add(p3);
p.add(p4);
Book b = new Book("harrypotter", BookCategory.NOVEL);
b.setPages(p);
b.getPages().get(0).getPageNum();
}
}
private int pageNum;
public Page(int pageNum) {
this.pageNumber = pageNum;
}
public int getPageNumber() {
return pageNum;
}
}