2017-07-20 39 views
-3

我正在尋找使用HashMap來存儲Block類中的屬性,以使整個屬性過程更加高效和靈活。我到目前爲止基本上是一個BlockProperty類,它將數據「內部」存儲爲一個字符串,並通過檢查「屬性類型」的值,使用正確的解析器(double,float,int,boolean等)進行解析一個最終數組作爲列表的靜態類。使用HashMap來存儲所有基元類型的屬性

我希望能夠請求某個屬性的類中的值,它會返回其適當的原始類型的值。

屬性類型列表 - 可以通過獲取查找索引和返回索引值的方法以任何順序重新排列。

protected static final String[] type = {"int","float","long","short","boolean","String","double","Object"}; 

Block Property If-statements。

public Object getAsPrimitiveData() 
{ 
    if(!isObjectType) 
    { 
     if(propertyType == BlockPropertyTypes.getIndexWithType("String")) 
      return internalPropertyData; 
     else if(propertyType == BlockPropertyTypes.getIndexWithType("int")) 
      return processIntType(); 
     else if(propertyType == BlockPropertyTypes.getIndexWithType("short")) 
      return processShortType(); 
     else if(propertyType == BlockPropertyTypes.getIndexWithType("long")) 
      return processLongType(); 
     else if(propertyType == BlockPropertyTypes.getIndexWithType("boolean")) 
      return processBooleanType(); 
     else if(propertyType == BlockPropertyTypes.getIndexWithType("float")) 
      return processFloatType(); 
     else 
      throw new IllegalArgumentException("Property type is invalid."); 
    } 

過程****類型全部調用類似於下面的方法,但針對它們各自的類型進行修改。 (額外輸出用於調試)

public float processFloatType() 
{ 
    try 
    { 
     return Float.parseFloat(internalPropertyData); 
    }catch(Exception e) 
    { 
     Notifier.printException(e); 
     Notifier.cwarn("Property Type and Property Data do not match types! " + this.propertyType + " == " + BlockPropertyTypes.getIndexWithType("float") + "?? returning -1;"); 
     return -1; 
    } 
} 
+2

郵編:我們不介意這裏的讀者。 –

+0

我的歉意。讓我知道你是否需要更多 –

+2

我們現在需要的是一個實際的問題。 – EJP

回答

0

考慮使用數

int a = 5; 
    float f = 5.5f; 
    double d = 5.5; 

    Number na = a; 
    Number nf = f; 
    Number nd = d; 

    System.out.println(na.getClass()); 
    System.out.println(nf.getClass()); 
    System.out.println(nd.getClass()); 

輸出

類java.lang.Integer

類java.lang.Float

class java.lang.Double

相關問題