问题描述
短语偏爱组成而不是继承是否适用于Swing组件?我想收集一些关于该主题的专业意见,并在继续设计UI之前更容易维护哪些代码。
Does the phrase "favor composition over inheritance" apply to Swing components? I'd like to gather some professional opinions about the subject and which code is easier to maintain before I go ahead and design a UI.
推荐答案
是。
我唯一扩展Swing组件的时间是当我需要重写一个或多个方法时。例如,当我想重写paintComponent方法时,我将扩展一个JPanel。
The only time I extend a Swing component is when I need to override one or more of the methods. For instance, I'll extend a JPanel when I want to override the paintComponent method.
在所有其他情况下,我的类都将包含我需要的Swing组件。这使我可以分离类方法:
All other times, my class will contain the Swing component(s) I need. This allows me to separate my class methods:
frame.getMethod();
来自Swing组件类方法:
from the Swing component class methods:
frame.getFrame().getPreferredSize();
这篇关于Java Swing:是否使用短语“在继承时偏爱组成”?应用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!