为什么许多SWT控件不允许子类化? [英] Why is subclassing not allowed for many of the SWT Controls?
问题描述
我经常发现自己想要这样做。当你想要存储一些有用的信息或额外的状态时,它会非常有用。
I often find myself wanting to do it. It can be very useful when you want to store some useful information or extra states.
所以我的问题是,是否有一个非常好的/强有力的理由为什么禁止这样做?
So my question is, is there a very good/strong reason why this is forbidden?
谢谢
编辑:
非常感谢所有这些答案。所以听起来对此没有正确或错误的答案。
Thanks a lot for all these answers. So it sounds like there's no right-or-wrong answer to this.
假设我接受这些类不是子类的事实,那么不标记Control类final,但是禁止子类化 - 有效地将异常/错误从编译时降级到运行时?
Assuming I accept the fact that these classes are not to be subclassed, what's the point of not marking a Control class final, but prohibiting subclassing - effectively demoting the exception/error from compile-time to run-time?
编辑^ 2:
请参阅我自己对此的回答:显然,这些类是可重写的,但需要由覆盖者明确确认。
EDIT^2: See my own answer to this: apparently, these classes are overrideable, but requires explicit acknowledgement by the overrider.
谢谢
推荐答案
看起来没有人在任何答案中提到这一点,但SWT确实提供了一个可重写的checkSubclass()方法,并且正是无法解决的问题抛出异常。要强制覆盖,您可以将方法覆盖为无操作,并有效地扩展合法性。我想让这个选项保持打开最终是因为该类没有成为最终的,并且扩展错误没有编译时而不是运行时。
It doesn't look like anybody mentioned this in any of the answers, but SWT does provide a checkSubclass() method that is overrideable and is precisely where the Unextenable exception is thrown. To force an override, you are allowed to override the method to a no-op and effectively make extending legal. I guess to leave this option open is ultimately the reason that the class is not made final and the extension error not made compile-time instead of run-time.
这篇关于为什么许多SWT控件不允许子类化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!