[英]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.