【发布时间】:2014-01-02 23:12:45
【问题描述】:
是否可以让类从泛型类型扩展?
我想使用implements 或extends 对不同的组件进行子类化,以便为所有子类添加统一的功能。
我想要实现的是:
class MyTable extends MyAbstractComponent<JTable> {...}
class MyPanel extends MyAbstractComponent<JPanel> {...}
MyAbstractComponent t = new MyTable();
MyAbstractComponent p = new MyPanel();
container.add(t);
container.add(p);
在这种情况下,我将如何制定MyAbstractComponent?
我尝试了以下方法,但它给了我错误“找到类型:参数 C。预期:类”:
abstract class MyAbstractComponent<C extends Component> extends C {...}
【问题讨论】:
-
你想用
C做什么? -
@SotiriosDelimanolis 让子类从 C 扩展。例如,我可以在 MyTable 中执行 getSelectedRows(),因为 C 是那里的 JTable。
标签: java generics abstract-class