2016-04-25 122 views
1

我試圖自己實現類似枚舉的東西。當我試圖迭代我的​​常量對象時,我遇到了使用反射,我偶然發現了java.lang.reflect.Field。所以這是我的場景。我有拿着一雙字符串的實體類常量通過接口中的對象常量進行迭代的迭代

public class ConstantEntity { 

    private String constantName; 
    private String constantDesc; 

    ConstantEntity(String name, String desc) { 
     this.constantName = name; 
     this.constantDesc = desc; 
    } 

    public String constantName() { 
     return this.constantName; 
    } 

    public String constantDesc() { 
     return this.constantDesc; 
    } 

} 

而且我有一個界面,我創建使用實體

public interface MyConstantsPool { 

    public static final ConstantEntity CONSTANT_ONE = new ConstantEntity("bla1", "11"); 
    public static final ConstantEntity CONSTANT_TWO = new ConstantEntity("bla2", "12"); 
    public static final ConstantEntity CONSTANT_THREE = new ConstantEntity("bla3", "13"); 


} 

常量和我試圖消耗,並通過這些常量迭代使用

import java.lang.reflect.Field; 

public class ConsumeConstants { 

    public static void main(String args[]) { 

     Field[] fields = MyConstantsPool.class.getDeclaredFields(); 
     for (int i = 0; i < fields.length; i++) { 

      Object myConstant = fields[i]; 
      ConstantEntity typeSafeEnum = (ConstantEntity) myConstant ; 
      System.out.println(" The name: " 
        + ((ConstantEntity) typeSafeEnum).constantName()); 
      System.out.println(" The description: " 
        + ((ConstantEntity) typeSafeEnum).constantDesc()); 

     } 
    } 
} 

我經歷了文檔,但我無法把握Field背後的想法。我對使用反思的理解完全錯誤嗎?我們什麼時候使用Field?在接口中迭代所有Object常量的正確方法是什麼?

NOTE:我正在使用java 1.4;所以我必須使用基本的Java功能來實現這一點。

回答

0

Field是一個訪問,它可以讓你設置和獲取操作,並做檢查,發現一樣在運行時的類型或領域的改性劑。

什麼是正確的方式來遍歷界面中的所有對象常量?

如果你想要遍歷所有static ConstantEntity領域,你可以使用這樣的事情:

Field[] fields = MyConstantsPool.class.getFields(); 
for (int i = 0; i < fields.length; i++) { 
    Field f = fields[i]; 
    if (ConstantEntity.class.isAssignableFrom(f.getType()) 
      && Modifier.isStatic(f.getModifiers())) { 
     ConstantEntity constant = 
      (ConstantEntity) f.get(null); 
    } 
} 

我是在使用反射的理解完全錯誤的?

從代碼中的錯誤,試圖將myConstant直接投射到ConstantEntity,至少部分。您必須致電get實際檢索該字段中保存的值。對象本身只是一個訪問器。

您還可以看到"What is reflection and why is it useful?"


此外,Java 1.4中,真的???這已經過了10年以上。

+2

爲什麼選擇倒票? – Pshemo

+1

接口不能有非靜態字段,所以'Modifier.isStatic(f.getModifiers()')可能是多餘的(除非我誤解了你的想法) – Pshemo

+0

@Pshemo是的,你說得對,在這裏檢查修飾符更多我的代碼示例只是打算更具普遍性。 – Radiodef

0

使用Java反射,您可以檢查類的字段(成員變量)並在運行時獲取/設置它們。

給定一個類的實例,可以使用反射來設置該類中字段的值。這通常只在 特殊情況下才能以通常方式設置值時不是 。 因爲此類訪問通常違反了該類的設計意圖 ,所以應該儘可能使用它。

因此一個類對象可以用來獲得Field對象的聲明爲public,聲明爲受保護或聲明私有字段。

但是,在你的情況下,它可能對你的界面的'設計意圖'有用。

+0

@Radiodef「我通過文檔去,但是我無法理解Field背後的想法,我對使用反射的理解完全錯誤嗎?我們什麼時候使用Field?「 我將問題解釋爲:「爲什麼使用反射?何時有用?目標是什麼?」 我錯了嗎? – granmirupa

0

爲什麼你不能使用數組而不是反射?

public static final ConstantEntity CONSTANT_ONE = new ConstantEntity("bla1", "11"); 
public static final ConstantEntity CONSTANT_TWO = new ConstantEntity("bla2", "12"); 
public static final ConstantEntity CONSTANT_THREE = new ConstantEntity("bla3", "13"); 

public static final ConstantEntity[] CONSTANTS = { 
    CONSTANT_ONE, 
    CONSTANT_TWO, 
    CONSTANT_THREE 
}; 
1

你只是做簡單的錯誤, 你搶Field,並試圖將其投放到您的ConstantEntity類,

你需要從外地獲取價值:

Object myConstant = fields[i].get(null);