Java 我无法访问类型类

Java 我无法访问类型类,java,Java,我有一个流派类,其中它有变量流派作为字符串,并有各自的getter、setter和toString方法 类型类如下所示: public class Genre(){ private string genre; //The constructor which takes parameter genre and assigns to genre; //The respective getters, setters and toString function; } 我还有一个

我有一个流派类,其中它有变量流派作为字符串,并有各自的getter、setter和toString方法

类型类如下所示:

public class Genre(){
    private string genre;
    //The constructor which takes parameter genre and assigns to genre;
    //The respective getters, setters and toString function;
}
我还有一个叫做Catalog的类,它有以下方法:

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.
}

当我添加上述所有书籍时,我可以从books类获得所有信息,但不能从体裁类获得。例如,我可以得到书名、书价、图书类型和书年。但是从体裁课上,我找不到任何体裁

当我运行上面的函数时,我不会在for-each循环的内部得到输出,但会在for-each循环的外部得到输出

我不知道为什么会这样

因为我有相同的函数和图书类,但我可以在目录类中获得图书类的所有信息,但不能从流派类中获得


为什么我不能从目录中获取流派信息?

正如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
    }

体裁:体裁{您从未在流派列表中添加过任何内容。@Johnnymop抱歉!拼写错误。我已编辑了问题。希望您现在理解。谢谢。亲爱的Pramish,欢迎使用StackOverflow。您似乎使用Book类的构造函数添加了新的流派实例。但它们可能不会添加到您的流派列表中。请同时发布Book类的构造函数ctor.@luksch谢谢你的回答。我已经添加了Book类及其构造函数。谢谢你的回答。我还有一个函数,我必须在其中添加书籍。这个方法不会动态添加流派。