Java 泛型/类型调度问题
考虑以下程序:
import java.util.List;
import java.util.ArrayList;
public class TypeTest {
public static class TypeTestA extends TypeTest {
}
public static class TypeTestB extends TypeTest {
}
public static final class Printer {
public void print(TypeTest t) {
System.out.println("T");
}
public void print(TypeTestA t) {
System.out.println("A");
}
public void print(TypeTestB t) {
System.out.println("B");
}
public <T extends TypeTest> void print(List<T> t) {
for (T tt : t) {
print(normalize(tt.getClass(), tt));
}
}
private static <T> T normalize(Class<T> clz, Object o) {
return clz.cast(o);
}
}
public static void main(String[] args) {
Printer printer = new Printer();
TypeTest t1 = new TypeTest();
printer.print(t1);
TypeTestA t2 = new TypeTestA();
printer.print(t2);
TypeTestB t3 = new TypeTestB();
printer.print(t3);
System.out.println("....................");
List<TypeTestB> tb1 = new ArrayList<TypeTestB>();
tb1.add(t3);
printer.print(tb1);
}
}
main 方法现在打印:
T
A
B
....................
T
我应该做什么才能让它打印以下内容?
T
A
B
....................
B
我想避免为每种可以打印的类型编写如下循环:
public void printTypeTestB(List<TypeTestB> t) {
for (TypeTestB tt : t) {
print(tt);
}
}
Consider the following program:
import java.util.List;
import java.util.ArrayList;
public class TypeTest {
public static class TypeTestA extends TypeTest {
}
public static class TypeTestB extends TypeTest {
}
public static final class Printer {
public void print(TypeTest t) {
System.out.println("T");
}
public void print(TypeTestA t) {
System.out.println("A");
}
public void print(TypeTestB t) {
System.out.println("B");
}
public <T extends TypeTest> void print(List<T> t) {
for (T tt : t) {
print(normalize(tt.getClass(), tt));
}
}
private static <T> T normalize(Class<T> clz, Object o) {
return clz.cast(o);
}
}
public static void main(String[] args) {
Printer printer = new Printer();
TypeTest t1 = new TypeTest();
printer.print(t1);
TypeTestA t2 = new TypeTestA();
printer.print(t2);
TypeTestB t3 = new TypeTestB();
printer.print(t3);
System.out.println("....................");
List<TypeTestB> tb1 = new ArrayList<TypeTestB>();
tb1.add(t3);
printer.print(tb1);
}
}
The main method now prints:
T
A
B
....................
T
What should I do to make it print the followings?
T
A
B
....................
B
I'd like to avoid writing a loop such as the following for each of the type that can be printed:
public void printTypeTestB(List<TypeTestB> t) {
for (TypeTestB tt : t) {
print(tt);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题的根源在于 Java 方法重载是在编译时根据方法参数表达式的声明类型来解决的。您的程序似乎正在尝试使用运行时分派到不同的方法重载。这在 Java 中根本行不通。
您在示例中使用泛型的事实有点转移注意力。如果将类型参数
替换为TypeTest
,也会遇到同样的问题。The root of your problem is that Java method overloads are resolved at compile time based on the declared type of the method argument expressions. Your program seems to be trying to use runtime dispatching to different method overloads. That simply doesn't work in Java.
The fact that you are using generics in your example is a bit of a red herring. You would have the same problem if you replaced the type parameter
<T>
withTypeTest
.考虑创建一个了解所有相关子类型的访问者界面。
这
应该打印出您想要的内容:
类型在允许编译时重载的接口中列出。另一方面,这是您放置不想参数化行为的子类型的单点。 Java 不是一种非常动态的语言...:)
Concider creating a visitor interface which knows about all relevant subtypes.
}
This should print out what you wanted:
The types are listed in the interface which allows for compile-time overloading. On the other hand, this is a single point where you have put the subtypes for which you wan't to parameterize behavior. Java is not a very dynamic language... :)
复杂的“访客模式”的候选者。
或者简单地将 print() 方法从
Printer
移至TypeTest
candidate for the convoluted "visitor pattern".
or simply move print() method from
Printer
toTypeTest