Java 如何将枚举值传递给构造函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20069410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:51:47  来源:igfitidea点击:

How to pass an enum value to a constructor

javaconstructorenums

提问by Pavlos Panteliadis

protected enum Category { Action, Fiction, Drama, Romance, SciFi, Poems, Kids } 

I have created this enumtype, and now I have to make a constructor for my class.

我已经创建了这种enum类型,现在我必须为我的类创建一个构造函数。

public Book(String title, int code, List<String> authors, String publisher, int year, Category categ){
        this.title = title;
        this.code = code;
        this.authors = authors;
        this.publisher = publisher;
        this.year = year;
        this.category = ....;
}

I don't understand how I will pass to the constructor the value of the enumerated type.

我不明白如何将枚举类型的值传递给构造函数。

Can anyone help?

任何人都可以帮忙吗?

I know it's beginners question, but I can't seem to find an answer anywhere.

我知道这是初学者的问题,但我似乎无法在任何地方找到答案。

采纳答案by Suresh Atta

Something like this

像这样的东西

new Book( title, ........ ,Category.anyEnumConstant);

For ex:

例如:

   Book book=  new Book( title, ........ ,Category.Fiction);

Then inside the constructor

然后在构造函数里面

 this.category = categ;

回答by Leonard Brünings

public Book(String title, int code, List<String> authors, String publisher, int year, Category categ){
        // ...
        this.category = categ;
}

Then call

然后打电话

new Book(/* ... */, Category.Action)

回答by SudoRahul

You can either send the enum as such or send a String and use the valueOf() to get the Enum.

您可以发送枚举,也可以发送字符串并使用 valueOf() 获取枚举。

Solution 1:Send the enum as such.

解决方案 1:发送枚举。

new Book(title, code, authors, publisher, year, Category.Action);

and in your constructor,

在你的构造函数中,

public Book(String title, int code, List<String> authors, String publisher, int year, Category categ){
    ...
    this.category = categ;
}

Solution 2:Send a string value and use the valueOf()to get the enum from it.

解决方案 2:发送一个字符串值并使用valueOf()从中获取枚举。

new Book(title, code, authors, publisher, year, "Action");

and in your constructor,

在你的构造函数中,

public Book(String title, int code, List<String> authors, String publisher, int year, String categString){
    ....
    this.category = Category.valueOf(categString);
}

回答by Yogesh Verma

Here is simple assignment of enum Constant value to a enum variable.

这是将枚举常量值简单分配给枚举变量的过程。

String title= "somevalue";
int code = 1;
ArrayList<String> arrayList = new ArrayList<String>();
String publisher = "somevalue";
int year=2013;
Category categ = Category.Action;
Book book = new Book(title, code, arrayList, publisher, year, categ);

In Enums we just use the enums Object Constants declared in Enum declaration. Actually they are the objects of your enum. here is a link where you can find a simple example to explore enums. http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

在枚举中,我们只使用枚举声明中声明的枚举对象常量。实际上,它们是您枚举的对象。这是一个链接,您可以在其中找到一个简单的示例来探索枚举。 http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

and in Constructor declaration you just simply have to assign the value like other variables.

在构造函数声明中,您只需像其他变量一样分配值。

this.category = categ;