我想實現並重寫不同返回類型的方法,而不必強制轉換返回類型。不用返回類型的java中的抽象方法沒有鑄造
public abstract class A {
public abstract Object getValue(String content);
}
public class B extends A {
public String getValue(String content) {...}
}
public class C extends A {
public int getValue(String content) {...}
}
public class D extends A {
public boolean getValue(String content) {...}
}
// Main loop:
for (A a : allAs)
{
// I want to use the method getValue() and corresponding to the type return a String, int or boolean without casting the return type
}
我的問題: 是否有可能無需被迫施放返回不同類型的? 抽象方法如何解決問題?
我認爲必須有一個解決方案,因爲編譯器應該知道的返回類型...
您不能基於返回類型進行覆蓋。 – 2013-02-23 06:37:57
而這個程序不會編譯 – 2013-02-23 06:38:23