简体   繁体   English

如何获取“ Enum.CONSTANT”以返回某种类型(例如,颜色)?

[英]How do I get “Enum.CONSTANT” to return some type (e.g. Color)?

I want the field constants of my enum to return instances of the Color class instead of some constant values, is this possible in Java? 我希望枚举的字段常量返回Color类的实例而不是某些常量值,这在Java中可能吗? I would like to avoid having to retrieve them through an accessor method. 我想避免必须通过访问器方法检索它们。 For example, this is the way I want to be able to use my enum: 例如,这就是我希望能够使用枚举的方式:

Color color;
color = ExtendedColor.RED;
color = ExtendedColor.FOO;
color = ExtendedColor.BAZ;

import java.awt.*;

/**
 * Static utility enum for providing a much greater variety of Colors to choose from, some of which are not
 * included as constants in the Color class
 */
public enum ExtendedColor {
    RED(Color.RED),
    ORANGE(Color.ORANGE),
    YELLOW(Color.YELLOW),
    FOO(10, 20, 30),
    BAR(90, 90, 90),
    BAZ(30, 30, 30);

    private Color color;

    private ExtendedColor(Color color) {
        this.color = color;
    }

    private ExtendedColor(int r, int g, int b) {
        this.color = new Color(r, g, b);
    }
}

You can create a class with public static fields instead and access the fields directly ExtendedColor.COLOR1 . 您可以改用具有公共静态字段的类,并直接访问ExtendedColor.COLOR1的字段。

import java.awt.*;

public class ExtendedColor {
    public static final Color COLOR1 = ...;
    public static final Color COLOR2 = ...;
    ...
}

One approach is given in the first answer. 第一个答案给出了一种方法。 Another approach is to define a colorValue method in the ExtendedColor enum which returns the actual Color. 另一种方法是在ExtendedColor枚举中定义colorValue方法,该方法返回实际的Color。

public enum ExtendedColor {
  ...
  public Color colorValue() {
    return color;
  }
}

And then you will have 然后你将有

Color color;
color = ExtendedColor.RED.colorValue();
color = ExtendedColor.FOO.colorValue();
color = ExtendedColor.BAZ.colorValue();

暂无
暂无

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

相关问题 在FreeMarker中,如何自动将自定义对象(例如java.awt.Color)转换为特定的String值(例如HTML十六进制颜色)? - In FreeMarker, how do I automatically convert a custom object (e.g. java.awt.Color) to a particular String value (e.g. HTML hex color)? 如何从 LinkedHashMap 集合中获取双数组的键集(例如数组 [double]) - How do I get a keyset of double arrays (e.g. array[double]) from a LinkedHashMap set Java:如何获得一个月中x天的日期(例如2012年2月的第三个星期一) - Java: How do I get the date of x day in a month ( e.g. Third Monday in February 2012) 在从文件读取更好的数据行之前,枚举有多复杂(例如.csv)? - How complex can an enum get before reading lines of data from a file is better (e.g. .csv)? 如何检查数组的所有元素是否不等于某个值? (例如,不是空的) - How can I check if all elements of array doesn't equal to some value? (e.g. not empty) 如何为BitSet类型的元素创建SortedSet(例如TreeSet) - How to create SortedSet (e.g. TreeSet) for elements of type BitSet 如何在Java中查看内置类的源代码(例如BigInteger等)? - How do I view source code of built-in classes in Java (e.g. BigInteger etc.)? 我如何从Locale.getISOCountries()获得“ en_US”; - How can I get e.g. “en_US” from Locale.getISOCountries(); 如何获得 boolean XPath 表达式的结果(例如“假”)? - How do I obtain the result of a boolean XPath expression (e.g. 'false')? 如何告诉Java将逻辑字体(例如SansSerif)映射到系统上的特定字体? - How do I tell Java to map a logical font (e.g. SansSerif) to a specific font on my system?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM