假设我有这些抽象类:
class BasicObject {
private String name;
// getter and setter here
}
class BasicObjectExplorer {
void explore(BasicObject basicObject) {
// now some actions regarding name property
}
}
现在你有AdvancedObject扩展BasicObject和AdvancedObjectExplorer扩展BasicObjectExplorer,如何使用泛型重用Basic类的代码,只编写有关添加到AdvancedObject的新属性的功能?
感谢您的帮助。
答案 0 :(得分:2)
如果您要求您的抽象类是处理泛型类的类的基础。
然后
public abstract class AbstractObjectExplorer<T> {
void explore(T basicObject) {
// now some actions regarding name property
}
}
和
public class Foo extends BasicObjectExplorer<String> {
}
public class Bar extends BasicObjectExplorer<Double> {
}