简体   繁体   English

Java枚举作为构造函数传递

[英]Java enum pass as an constructor

I am trying to pass a reference of an enum type from a constructor to test some lambdas, method reference, and Stream s. 我试图从构造函数传递枚举类型的引用来测试一些lambdas,方法引用和Stream s。 When I try to instantiate the class I am getting a error on the enum. 当我尝试实例化类时,我在枚举上遇到错误。

public class Book {

    private String title;
    private List<String> authors; 
    private int pageCount[];
    private Year year; 
    private double height;
    private Topic topic;

    public Book (String title, ArrayList<String> author, int pageCount [], Year year, double height, Topic topic  )
    {
        this.title = title;
        this.authors = author;
        this.pageCount = pageCount; 
        this.topic = topic;
        this.year = year;
        this.height = height;
    }

    public enum Topic
    {
        MEDICINE, COMPUTING, FICTION, HISTORY

    }

    public String getTitle(){
        return title;
    }

    public List<String> getAhuthors(){
        return authors;
    }

    public int [] getPageCounts(){
        return pageCount;

    }

    public Topic getTopic()
    {
        return topic;
    }



    public Year getPubDate(){
        return year;
    }

    public double getHeight()
    {
        return height;
    }

    public static void main(String args [] )
    {


        Book nails = new Book("Fundamentals of Chinese Fingernail image", Arrays.asList("Li", "Fun", "Li"), 
                 new int [] {256}, Year.of(2014), 25.2, COMPUTING);

        Topic test = Topic.COMPUTING;

        System.out.println(test);
    }
}

this is what I am getting : 这就是我得到的:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
     COMPUTING cannot be resolved to a variable at Book.main(Book.java:70)

Replace 更换

Book nails = new Book("Fundamentals of Chinese Fingernail image", Arrays.asList("Li", "Fun", "Li"), 
             new int [] {256}, Year.of(2014), 25.2, COMPUTING);

by 通过

Book nails = new Book("Fundamentals of Chinese Fingernail image", Arrays.asList("Li", "Fun", "Li"), 
             new int [] {256}, Year.of(2014), 25.2, Topic.COMPUTING);

EDIT: 编辑:

Like @Voltboyy pointed out, you must change qhat i pointed out before and replace the ArrayList<String> in the constructor to List<String> like this: 就像@Voltboyy指出的那样,你必须改变我之前指出的qhat并将构造函数中的ArrayList<String>替换为List<String>如下所示:

public Book(String title, List<String> list, int pageCount[], Year year, double height, Topic topic) {
    this.title = title;
    this.authors = list;
    this.pageCount = pageCount;
    this.topic = topic;
    this.year = year;
    this.height = height;
}

and you program will work. 你的程序会起作用。

As said by @djointster, COMPUTING should be Topic.COMPUTING . 正如@djointster所说, COMPUTING应该是Topic.COMPUTING

But your code will still not work afterwards as private List<String> authors differs from ArrayList<String> author in your constructor. 但是,之后您的代码仍无效,因为private List<String> authors与构造函数中的ArrayList<String> author private List<String> authors不同。

So you should change this: 所以你应该改变这个:

ArrayList<String> authors;
//to
List<String> authors;

in the constructor. 在构造函数中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM