这个类的构造我有问题。我想以某种方式将超类Super2的扩展类类型作为Super1构造函数中的参数,但我不知道如何实现。
我想满足这些要求:
abstract class Super2 {
Super2(String test) {
}
public void exit() {
}
}
abstract class Super1 {
abstract doStuff(Class<? extends Super2> super);
protected doStuffWithDoStuff() {
Super2 super2 = new Super2("test");
doStuff(super2);
super2.exit();
}
}用法:
class Test1 extends Super2 {
Test1() {
super();
}
public void doStuff() {
}
}
class Test2 extends Super1 {
doStuff(Test1 test) {
test.doStuff();
}
}发布于 2015-12-13 04:20:11
我认为你的问题太深奥了,无法理解你到底想达到什么目的。以下工作(符合您所描述的要求,我相信)会吗?
abstract class Super1 {
private Class<? extends Super2> super2Type;
protected Super1(Class<? extends Super2> super2Type) {
this.super2Type = super2Type;
}
abstract doStuff(Class<? extends Super2> super);
protected doStuffWithDoStuff() {
Super2 super2 = super2Type.newInstance(); //Error handling omitted for brevity
doStuff(super2);
super2.exit();
}
}https://stackoverflow.com/questions/34135609
复制相似问题