Java Swing:实现 TableModel 还是扩展 AbstractTableModel?
我什么时候应该实现 TableModel,什么时候应该扩展 AbstractTableModel?
When should I rather implement TableModel and when should I extend AbstractTableModel?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您需要表模型的完整干净的实现和/或您必须提供模型本质上尚不存在或不适合的东西时。
当您可以重用抽象类提供的现有结构和方法和/或者您可以更轻松地实现它时。
我们的想法是,如果你可以重复使用它,那就这样做。如果做不到,请从头开始实施。
通过继承 AbstractTableModel,您无论如何都将实现 TableModel 接口,而且会更容易。
When you need a complete clean sheet implementation of the table model and/or you have to provide something that by the nature of your model doesn't exists yet or doesn't fit.
When you can reuse the existing structure and methods provided by the abstract class and/or it would easier for you to implement it.
The idea is, if you can re-use it, do so. If you can't, implement from scratch.
By inheriting AbstractTableModel you'll be imlementing the TableModel interface anyway, it just will be easier.
AbstractTableModel
具有处理TableModelListeners
的实现,包括触发TableModelEvent
。如果你想自己处理这个问题,那么确实没有理由延长。在该代码之外,除了对接口中声明的方法进行存根之外,其他代码不会添加任何好处。AbstractTableModel
has implementation for the handling ofTableModelListeners
, including the firing ofTableModelEvent
s. If you want to handle that yourself, then there really is no reason to extend. Outside of that code, the other code doesnt add any benefit other than the stubbing out of methods declared in the interface.AbstractTableModel 为 TableModel 中定义的许多方法提供默认实现。如果您不需要任何自定义行为,那么您可以简单地扩展 AbstractTableModel。另外,如果您的模型已经扩展了不同的基类,那么仅实现 TableModel 就合适了。
AbstractTableModel provides default implementations for many of the methods defined in TableModel. If you don't need any custom behavior, then you can simply extend AbstractTableModel. Also, if your model already extends a different base class, then it would be appropriate just to implement TableModel.