[英]Java enum synthetic arguments for constructors
请看一下综合论证 。 枚举构造函数有两个附加的综合参数。
请查看以下部分:
另一个示例:Java枚举类
如您所见,它节省了很多代码,但还添加了合成字段,方法和构造函数参数。 如果您定义了自己的构造函数,则带有其自己的参数集。
可能存在枚举构造函数没有任何综合参数的情况。
抱歉,没有提供足够的细节。
看完这篇文章,我会说答案是否定的。 本文介绍了一个典型的枚举,例如:
enum Colours {
RED, BLUE;
}
变为:
final class Colours extends java.lang.Enum {
public final static Colours RED = new Colours("RED", 0);
public final static Colours BLUE = new Colours("BLUE", 1);
private final static values = new Colours[]{ RED, BLUE };
private Colours(String name, int sequence){
super(name, sequence);
}
public static Colours[] values(){
return values;
}
public static Colours valueOf(String name){
return (Colours)java.lang.Enum.valueOf(Colours.class, name);
}
}
Colours
构造函数的参数被认为是综合的 (即,它们是由编译器产生的,以确保“工作原理”)。 因此,综合论证似乎是不可避免的,因为它们是将枚举转换为真实类的必要部分。
唯一的可能性是,如果枚举没有值-Java是否仍会创建合成字段? 凭直觉,答案是肯定的。 OK中的文章对此提供了支持,但是我为什么要关心呢? 部分。 作者在这里显示,当用反射查看时,空枚举的参数计数仍然为2。
检查TimeUnit的Concurrent类的源代码。 这是一个具有自己方法的枚举。 您可以像枚举类一样使用枚举。
http://fuseyism.com/classpath/doc/java/util/concurrent/TimeUnit-source.html
这是我的一个例子:
public enum ExampleEnum {
ENUM_1 ( "ENUM_1", 1, Color.GREEN ) {
@Override
public void doMethingWeird( String stringToEnum ) {
//Implementation goes here;
}
},
ENUM_2 ( "ENUM_2", 2, Color.BLACK ) {
@Override
public void doMethingWeird( String stringToEnum ) {
//Implementation goes here;
}
},
ENUM_3 ( "ENUM_3", 3, Color.WHITE ){
@Override
public void doMethingWeird( String stringToEnum ) {
//Implementation goes here;
}
}; //Don't forget the semicolon ';' after the enums, to separate them from the methods;
//You can have static constants;
private static final Object object = new Object();
private final String enumName;
private final int enumNumber;
private final Color enumColor; //why not?
//CONSTRUCTOR IT MUST BE PRIVATE
private Effect( String enumName, int enumNumber, Color enumColor ){
this.enumName = enumName;
this.enumNumber = enumNumber;
this.enumColor = enumColor;
}
//you can have abstract methods and implement them on the enums.
public abstract void public void doMethingWeird( String stringToEnum );
public String getEnumName() {
return enuName;
}
public int getEnumNumber() {
return enumNumber;
}
public Color getEnumColor() {
return enumColor;
}
}
我希望能有所帮助。
我遇到了同样的问题,有一个带有树构造函数和参数的枚举。 进行反射并获取构造函数参数,您将获得String和一个int extra作为前两个参数。 我想知道他们来自哪里。 调试后,我发现Enum类具有受保护的构造函数,该函数使用前两个参数。
我通过添加不带参数的构造函数进行了测试,并且还添加了两个额外的参数。
来自Enum.java的带有构造函数的代码:
/**
* Sole constructor. Programmers cannot invoke this constructor.
* It is for use by code emitted by the compiler in response to
* enum type declarations.
*
* @param name - The name of this enum constant, which is the identifier
* used to declare it.
* @param ordinal - The ordinal of this enumeration constant (its position
* in the enum declaration, where the initial constant is assigned
* an ordinal of zero).
*/
protected Enum(String name, int ordinal) {
this.name = name;
this.ordinal = ordinal;
}
要检测这种情况,可以使用反射构造函数的isEnum()方法,并跳过2个参数。
Constructor<?> constructor;
private boolean isEnum() {
return constructor.getDeclaringClass().isEnum();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.