2012-10-10 106 views
0

我需要爲我的應用程序創建一個元素庫。Java枚舉 - 返回字符串

這是我創建的類。現在

public class Elements 
{ 
    public enum Type1 
    { 
    A ("text1"), 
    B ("text2"), 
    C ("text3"), 
    D ("text4"), 
    E ("text5"); 

    private String identifier; 

    Type1(String identifier) 
    { 
     this.identifier = identifier; 
    } 

    getPath() 
    { 
     String path = ""; 
     //do something 
     return path; 
    } 
    } 
} 

我可以用Elements.type1.A.getPath();

我做Elements.type1的靜態導入訪問的價值觀和我想刪除的getPath()的使用,因爲它會我的代碼複雜化。即。我需要能夠使用type1.A

所以,我沒有,

public class Elements 
{ 
    public enum Type1 
    { 
    A 
     { 
     public String toString() 
     { 
      return getPath("text1"); 
     } 
     }, 
    B 
     { 
     public String toString() 
     { 
      return getPath("text2"); 
     } 
     }, 
    C 
     { 
     public String toString() 
     { 
      return getPath("text3"); 
     } 
     }; 

    Type1() {} 
    } 
} 

現在我可以用Elements.Type1.A與打印報表,但我有一個接受String作爲參數的方法。

因此,這使得它Elements.Type1.A.toString()。沒有toString(),它會拋出一個錯誤。

有沒有辦法擺脫toString()

編輯:新代碼

public Interface Type1 
{ 
    String A = "text1"; 
    String B = "text2"; 
    String C = "text3"; 
} 
public class Utility 
{ 
    public static void main(String[] args) 
    { 
    getPath(Type1.A); 
    } 
    public static void getPath(String arg) 
    { 
    //Constructs xpath - text1 changes to //*[contains(@class,'text1')] 
    return xpath; 
    } 
} 
public class myClass 
{ 
    public void doSomething() 
    { 
    assertEquals(Type1.A,xpath); 
    } 
} 

這裏,Type1.A返回 「text1」 中,而不是// * [包含(@類, '文本1')]

+1

您可以避免使用'toString()'的唯一原因通常是因爲'+'和System.out/System.err知道如何在封面。 –

+0

首先不要使用'toString'來返回除了'對象的字符串表示'之外的東西。這個班的客戶怎麼知道這實際上是一些'路徑'? 而對於你的問題,你可以重載你的方法採取枚舉參數,從中提取必要的屬性,並將其傳遞給原始方法。 – zvez

+0

對不起,這是我的錯。編輯代碼。 – aradhak

回答

3

看來你需要三個字符串常量不枚舉。

public static final String A = "test1"; 
public static final String B = "test2"; 
public static final String C = "test3"; 

使用的接口並不總是最好的,但沒有進一步的情況下,我不能提出更好的東西

public interface Type1 { 
    String A = "test1"; 
    String B = "test2"; 
    String C = "test3"; 
} 
+0

但我需要在返回值之前對值進行一些操作。那可能嗎? – aradhak

+0

此外,我需要使用Type1來表示A屬於Type1 – aradhak

+1

@Aswathi ..將它們轉儲到名爲'Type1'的接口..這就是你需要的.. –

3

嗯,Peter說,你需要final static變量這裏..

看起來,就像你想要一組String Constants與..一起工作..那麼肯定你應該與什麼Peter已經引用..

只是爲了延長他說,你可以創建一個接口,並在其所有的字符串常量。然後,你可以很容易地通過Interface名稱訪問他們..

public Interface Type1 { 
    String A = "text1"; 
    String B = "text2"; 
    String C = "text3"; 
} 

而你的地方其他類: -

public class Utility { 
    public static void main(String[] args) { 

     // You can pass your String to any method.. 
     String a = Type1.A; 

     getPath(a); 
     getPath(Type1.B);   

    } 

    // This method is not doing work according to its name.. 
    // I have just taken an example, you can use this method to do what you want. 
    public static void getPath(String arg) { 
     // You can process your string here. 
     arg = arg + "Hello"; 
    } 
} 

你也可以改變什麼根據您的需要你toString()回報..

並遵循Java Naming Convention ..枚舉類,以Uppercase字母開頭..

+0

謝謝,已編輯的代碼遵循命名約定。 – aradhak

+0

不可能爲枚舉中的每個值創建Type1對象,因爲它們會有100個。 – aradhak

+0

@Aswathi ..你到底想要什麼。你需要更具體一些。就我的評論而言,你想要一組String常量?我理解對嗎? –