2012-10-10 66 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常量?我理解对吗? –