2013-01-22 50 views
0

在這個方法中我得到字符串作爲輸入,並根據字符串名稱我需要返回值有時它的字符串有時int,double,int64,布爾等 由於它的動態類型我不' t知道如何在方法返回類型 中定義它,以及如何添加值以及如何調用此方法返回類型是動態的,任何想法?根據輸入切換並返回動態值

public static ? SwitchInput(String TypeName) { 

     if (TypeName == "java.lang.String") { 
      Return = "A"; 
     } 
     else if (TypeName == "int") { 
      Return = 1; 
     } 
     else if (TypeName == "double") { 
      Return = 1.00 
     } 

等爲布爾和所有其他類型

} 
+0

這只是僞代碼或者是有效的在任何語言?它絕對不是Java(例如,Java使用'equals'作爲'String'比較,...)。 – Neet

+0

閱讀[Peter Lawrey在這裏回答](http://stackoverflow.com/questions/5561436/can-two-java-methods-have-same-name-with-different-return-type)。 – moonwave99

回答

1

要知道,返回類型是什麼,你必須要找到的容器中所有這些類型都適合。顯然,這是Object。你必須將原始類型轉換爲相應的對象(如int到Integer)。

更好的方法是創建一個新的容器類,它包含一個通用類型<T>。像

public class SwitchDemo { 

public static SwitchInputType<?> switchInput(String typeName) { 
    if (typeName.equals("java.lang.String")) { 
     return new SwitchInputType<String>(new String("A")); 
    } else if (typeName.equals("int")) { 
     return new SwitchInputType<Integer>(new Integer(312)); 
    } 
    return null; 
} 

public static class SwitchInputType<T> { 
    private T type; 

    public SwitchInputType(T type) { 
     super(); 
     this.type = type; 
    } 

    public T getType() { 
     return type; 
    } 

    public void setType(T type) { 
     this.type = type; 
    } 
} 

public static void main(String[] args) { 
    SwitchInputType<?> sit1 = SwitchDemo.switchInput("java.lang.String"); 
    System.out.println(sit1.getType()); 
    SwitchInputType<?> sit2 = SwitchDemo.switchInput("int"); 
    System.out.println(sit2.getType()); 
} 

}

+0

你可以舉個例子,請爲通用類型? –

+0

鮑里斯:請參閱上面的編輯 – ratlan

+0

感謝ratlan,但您將如何調用switchInput,即如何從中獲取數據?再次感謝 –

1

作爲一個醜陋的解決你的問題,你可以設置你的方法來運行Object類型。 (如布爾型,整數型,雙精度型都是子類型)

您將不得不確保儘管在使用返回值(使用instanceof)並將其重新設置爲正確類型之後,您隨後推斷出正確的類型。

我可以問,爲什麼你需要這種方法?這略微濫用了方法定義的概念。

public static Object SwitchInput(String TypeName) { 

    if (TypeName.equals("java.lang.String")) { 
     Return = new String("A"); 
    } 
    else if (TypeName.equals("int")) { 
     Return = new Integer(1); 
    } 
    else if (TypeName.equals("double")) { 
     Return = new Double(1.00) ; 
    } 
etc for bool and all the other types 

} 

並使用此代碼片段來推斷它進一步是什麼類型的代碼中的

if(returned_value instanceof Double) 

+0

我需要這種方法創建值的類型,我得到例如,如果我得到字符串我需要返回像string1 string2等值,如果我得到int我需要返回值等類型名稱,因爲你認爲這是醜陋的解決方案你會如何做到這一點? –

2

Object將是你最好的選擇,除非返回類型股票的祖先就下來了

實施例:

public static Object switchInput(String typeName) {  

    if ("java.lang.String".equals(typeName)) { 
     return "A"; 
    } 
    else if ("int".equals(typeName)) { 
     return 1i; 
    } 
    else if ("double".equals(typeName)) { 
     return 1.0d 
    } 
} 

泛型又如

static <T> T switchInput(String typeName){ 
    if ("java.lang.String".equals(typeName)) { 
    return "A"; 
    } 
    else if ("int".equals(typeName)) { 
     return 1i; 
    } 
    else if ("double".equals(typeName)) { 
     return 1.0d 
    } 

} 

String str = MyClass.switchInput("java.lang.String") 

我沒有測試過,這是我第一個想到的一個簡化版本關於仿製藥

+0

你能提供一個答案的例子嗎? –

+0

謝謝我的意思,但你將如何從值的對象檢索數據? –

+0

我不明白你的意思。返回對象的運行時類是您想要的那個 – Grooveek