【发布时间】:2021-02-27 01:17:01
【问题描述】:
上下文
我正在使用带有 JPanel 等的 Swing 以及其他 JComponent 创建一个 RPG 字符生成器程序,这些组件根据依赖项列表以特定顺序返回它们的数据(见下文)。我希望组件包含一些额外的数据和功能,所以我可能想要扩展,比如
NewField 扩展了 JTextField
NewButton 扩展了 JButton
NewLabel 扩展了 JLabel 等等。
问题
我可以手动输入这些,当然!但是它们都会有共同的变量,比如“依赖列表”或“InitialOrder”等等。 我正在寻找一种能够提供该模板数据(包括函数和局部变量)的关系。我试过了……
- 接口,但不会创建非静态变量
- 扩展 JComponents 的超类,但不包括 JTextField 和 JButton 方法的细节。我必须在那个新课程中重新声明所有内容,而且我宁愿避免这样做。
- 创建 JComponent 对象并在需要时返回该对象的通用类,但这是一个纯粹的 JTextField 组件。 “getDependencies”不起作用,我仍然需要一个完整的列表。即使在整个情况下,设置限制也无济于事。
注意:依赖项只是必须首先读取的字段列表,因为用户可以在文本文件中定义条件来更改某些内容。例如,如果用户希望房屋数量在 50 到 100 之间,并且每所房屋大约 3 到 5 人,则 NUMBEROFHOUSES 字段必须在 NUMBEROFRESIDENTS 字段生成其值之前生成一个值,因为它必须这样做次数等于 NUMBEROFHOUSES 生成的值。 NUMBEROFRESIDENCES 的依赖关系列表将包含 NUMBEROFHOUSES,这意味着当读取所有字段的列表时,“GENERATE NUMBEROFHOUSES”字符串将位于“GENERATE NUMBEROFRESIDENCES”字符串之前。 依赖项只是一个“确保首先读取这些字段”列表
问题
像创建泛型对象的泛型一样,我可以定义一个扩展基于另一个变量或参数的类吗?
例如:类 NewComp 扩展 T
类 NewButton 扩展 NewComp
示例
当前方法
class NewButton extends JButton{
private ArrayList<String> dependencies;
NewButton(){super();}
public ArrayList<String> getDependencies{return dependencies;}
public void setDependencies(ArrayList<String> newDeps){dependencies = newDeps;}
}
class NewTextField extends JTextField{
private ArrayList<String> dependencies;
NewTextField (){super();}
public ArrayList<String> getDependencies{return dependencies;}
public void setDependencies(ArrayList<String> newDeps){dependencies = newDeps;}
public void aSpecificNewTextFieldMethod(){...}
}
class NewLabel extends JLabel{
private ArrayList<String> dependencies;
NewLabel (){super();}
public ArrayList<String> getDependencies{return dependencies;}
public void setDependencies(ArrayList<String> newDeps){dependencies = newDeps;}
public void firstNewLabelMethod(){...}
public void secondNewLabelMethod(){...}
}
class CompManager{
private JPanel p;
CompManager(JPanel p){
this.p = p;
p.add(new NewButton(), new NewTextField().setText("Fake"), new NewLabel());
Component[] c = p.getComponents();
for(Component comp: c){
if(comp.getClass().equals(NewField)) {//do something}
}
}
有问题的方法
class NewComp extends <T>{
private ArrayList<String> dependencies;
NewComp(){Super();}
public ArrayList<String> getDependencies{return dependencies;}
public void setDependencies(ArrayList<String> newDeps){dependencies = newDeps;}
}
class NewButton extends NewComp<JButton>{
NewButton(){super();}
}
class NewTextField extends NewComp<JTextField>{
NewTextField (){super();}
public void aSpecificNewTextFieldMethod(){...}
}
class NewLabel extends NewComp<JLabel>{
NewLabel (){super();}
public void firstNewLabelMethod(){...}
public void secondNewLabelMethod(){...}
}
class CompManager{
private JPanel p;
CompManager(JPanel p){
this.p = p;
p.add(new NewButton(), new NewTextField().setText("Fake"), new NewLabel());
Component[] c = p.getComponents();
for(Component comp: c){
if(comp.getClass().equals(NewField)) {//do something}
}
}
即使答案是否定的,我也很想听听一些替代解决方案! 如果我没有把问题说清楚,请说清楚!
【问题讨论】:
-
你过度思考你的问题,最重要的是过度使用继承,如果你继续走这条路,我认为你正在走向编码噩梦。更多地关注组合的使用、简单性和限制复杂性(圈复杂性)
-
例如,关于我使用和创建的组件(我已经使用过很多),我大约有 5% 的时间继承了 GUI 组件(主要是需要重写其paintComponent 来进行绘图的 JPanel ) 和其他 95% 的时间,使用直接的 GUI 组件,这些组件根据需要设置了属性,但不是继承的。如果需要许多具有相似属性的,我将通过工厂方法来完成。
-
还有建议,摆脱不必要的喋喋不休的问题,而是专注于让您的问题易于理解。一个小的可编译可运行代码示例将大大有助于实现这一目标。一定要浏览help center 链接、minimal reproducible example 链接和How to Ask 链接,看看如何改进这个和你未来的问题。
-
感谢您的反馈!虽然我认为设置和上下文可以解释我的思维过程,但清晰不会伤害!基本上我想重写 JComponent 类来添加更多的方法/变量。我会查找工厂方法,我以前见过一次弹出...再次感谢,注意安全!
-
另外,查找 M-V-C 设计模式(代表模型-视图-控制器),然后研究它。然后,您可能希望更多地关注模型而不是视图(您目前正在这样做)
标签: java swing oop polymorphism relationship