我有一个genre
类,其中它的变量string
作为getters
,并且具有各自的setters
,toString
和public class Genre(){
private string genre;
//The constructor which takes parameter genre and assigns to genre;
//The respective getters, setters and toString function;
}
方法。
流派类如下:
public class Catalogue(){
private List<Book> booksAvailable;
private List<Genre> genres;
public Catalogue(){
this.genres = new LinkedList<Genre>();
booksAvailable.add(new Book("Swift", 1999, new Genre("Programming"),20));
booksAvailable.add(new Book("TheAlChemist", 2000, new Genre("Drama"),20));
//Name of a book, year of publication, genre, price
}
public void getGenre(){
System.out.println("I am outside the for loop so I will get printed");
for (Genre genre : genres){
System.out.println("I am inside the fo each loop so I will not get printed.");
}
}
}
public class Book {
private String title;
private int year;
private Genre genre;
private int price;
public Book(String title, int year, Genre genre, int price) {
this.title = title;
this.year = year;
this.genre = genre;
this.price = price;
}
//Here we have getters and setters and toString function.
}
我还有一个名为Catalog的类,它具有以下方法:
objs = [
Entry.objects.create(headline='Entry 1'),
Entry.objects.create(headline='Entry 2'),
]
objs[0].headline = 'This is entry 1'
objs[1].headline = 'This is entry 2'
Entry.objects.bulk_update(objs, ['headline'])
当我添加以上所有书籍时,我可以从Books类而不是Genre类中获取所有信息。例如,我可以获取bookName,bookPrice,bookGenre和bookYear。但是从流派课程来看,如果流派我什么也听不懂。
运行上面的函数时,我不会在for-each循环内获取输出,但是会在for-each循环之外获取输出。
我不知道为什么会这样。
因为我确实具有与Book类相同的功能,但是我可以在目录类中(但不能从Genre类中)获得book类的所有信息。
为什么我不能从目录中获得类型信息?
答案 0 :(得分:2)
正如上面提到的JohnnyMopp一样,您没有在列表中添加任何类型。如下更改代码:
public Catalogue() {
this.genres = new LinkedList<Genre>();
Genre programming = new Genre("Programming");
Genre drama = new Genre("Drama");
this.genres.add(programming);
this.genres.add(drama);
booksAvailable.add(new Book("Swift", 1999, programming, 20));
booksAvailable.add(new Book("TheAlChemist", 2000, drama, 20));
//Name of a book, year of publication, genre, price
}