我正在尝试实现和覆盖具有不同返回类型的方法,而不必强制转换返回类型。
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
}
我的问题:是否可以在不强制转换的情况下返回不同的类型?抽象方法是如何解决问题的?
我认为必须有一个解决方案,因为编译器应该知道返回类型......