我有這樣的兩個階級,我想,第二個(DocumentiDiIdentitaList)創建一個基於在第一類中聲明的變量的所有值的列表(DocumentiDiIdentita)反思級Java
其實我得到這個代碼
public class DocumentiDiIdentita {
public static final String CARTA_IDENTITA = "CI";
public static final String CARTA_IDENTITA_AFTER_25_06_2003 = "CI_NEW";
public static final String PASSAPORTO_CONSOLATO = "PC";
public static final String PASSAPORTO_QUESTURA = "PQ";
public static final String PASSAPORTO_ESTERO = "PE";
public static final String PATENTE_MORIZZAZIONE = "PZ";
public static final String PATENTE_PREFETTURA = "PT";
public static final String PORTOARMI = "PM";
public static final String PASSAPORTO="PA";
}
public static class DocumentiDiIdentitaList {
private static Field[] fields = DocumentiDiIdentita.class.getFields();
public static List documentTypes = Arrays.asList(fields);
}
但實際上列表documentTypes cointains變量的名稱,而不是它們的值,所以實際上是:
CARTA_IDENTITA
CARTA_IDENTITA_AFTER_25_06_2003
PASSAPORTO_CONSOLATO
etc.
,但我想:
CI
CI_NEW
PC
etc.
我怎麼能做到這一點? (我必須在Java 1.4上運行)
Field.get(Object)呢? http://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#get-java.lang.Object- – JEY
@JEY使這個答案;) – 2016-04-26 07:56:37
我不能得到如何使用這個「Field.get(Object)」,對不起 – RudiDudi