如何将枚举值传递给构造函数 [英] How to pass an enum value to a constructor

查看:156
本文介绍了如何将枚举值传递给构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

任何人都可以帮忙吗?

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

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

推荐答案

这样的事情

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

例如:

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

然后在构造函数内部

 this.category = categ;

这篇关于如何将枚举值传递给构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆