在Java中,变量或参数可以设置子类的父类吗? 上下文
我正在使用Swing with JPanels等以及其他JComponents创建一个RPG角色生成器程序,这些组件根据依赖项列表以特定顺序返回数据(见下文)。我希望组件包含一些额外的数据和函数,所以我可能希望扩展,比如 NewField扩展了JTextField NewButton扩展了JButton NewLabel扩展了JLabel等等 问题 我可以手动输入这些,当然!但它们都有共同的变量,比如“依赖列表”或“初始顺序”等等我正在寻找一种能够提供模板数据、函数和局部变量的关系。我试过在Java中,变量或参数可以设置子类的父类吗? 上下文,java,swing,oop,polymorphism,relationship,Java,Swing,Oop,Polymorphism,Relationship,我正在使用Swing with JPanels等以及其他JComponents创建一个RPG角色生成器程序,这些组件根据依赖项列表以特定顺序返回数据(见下文)。我希望组件包含一些额外的数据和函数,所以我可能希望扩展,比如 NewField扩展了JTextField NewButton扩展了JButton NewLabel扩展了JLabel等等 问题 我可以手动输入这些,当然!但它们都有共同的变量,比如“依赖列表”或“初始顺序”等等我正在寻找一种能够提供模板数据、函数和局部变量的关系。我试过 接口
现行方法
类NewButton扩展了JButton{
私有ArrayList依赖项;
纽扣(){super();}
公共ArrayList getDependencies{return dependencies;}
public void setDependencies(ArrayList newDeps){dependencies=newDeps;}
}
类NewTextField扩展了JTextField{
私有ArrayList依赖项;
NewTextField(){super();}
公共ArrayList getDependencies{return dependencies;}
public void setDependencies(ArrayList newDeps){dependencies=newDeps;}
public void aSpecificNewTextFieldMethod(){…}
}
类NewLabel扩展了JLabel{
私有ArrayList依赖项;
NewLabel(){super();}
公共ArrayList getDependencies{return dependencies;}
public void setDependencies(ArrayList newDeps){dependencies=newDeps;}
public void firstNewLabelMethod(){…}
public void secondNewLabelMethod(){…}
}
班级经理{
私人JPanel p;
公司经理(JPanel p){
这个,p=p;
p、 添加(newnewbutton(),newnewtextfield().setText(“假”),newnewlabel());
组件[]c=p.getComponents();
用于(组件组件:c){
如果(comp.getClass().equals(NewField)){//do something}
}
}
有问题的方法
类NewComp扩展{
私有ArrayList依赖项;
NewComp(){Super();}
公共ArrayList getDependencies{return dependencies;}
public void setDependencies(ArrayList newDeps){dependencies=newDeps;}
}
类NewButton扩展了NewComp{
纽扣(){super();}
}
类NewTextField扩展了NewComp{
NewTextField(){super();}
public void aSpecificNewTextFieldMethod(){…}
}
类NewLabel扩展了NewComp{
NewLabel(){super();}
public void firstNewLabelMethod(){…}
public void secondNewLabelMethod(){…}
}
班级经理{
私人JPanel p;
公司经理(JPanel p){
这个,p=p;
p、 添加(newnewbutton(),newnewtextfield().setText(“假”),newnewlabel());
组件[]c=p.getComponents();
用于(组件组件:c){
如果(comp.getClass().equals(NewField)){//do something}
}
}
即使答案是否定的,我也希望听到一些替代方案!
如果我没有把问题讲清楚,请说出来
我可以定义一个扩展基于另一个变量或参数的类吗
是的,经过进一步思考,事实上,你可以。类似的东西被称为(请查看链接以获得关于这方面的详细教程)。这允许你创建具有特定特征的类的对象,这些特征可以在运行时确定,这就是我认为你正在尝试做的。因为工厂是“抽象的”(通常从一个接口开始),您可以根据需要和程序的发展灵活地创建多个具体版本,然后可以使用创建的每个具体工厂创建多个不同的组件(或逻辑实体)。它非常强大和灵活
话虽如此,我仍然支持我对您的原始帖子所作的大部分评论,包括:
- 您可能过度使用了继承,而不是专注于更易于使用的组合扩展
- 可能需要更多地关注程序的模型,即非GUI逻辑基础,它与构成程序另一个主要部分的具体GUI组件(视图)分离
- 至于Swing,我仍然建议不要嵌入您创建和使用的大多数Swing组件,而是使用“开箱即用”的组件,而是根据需要设置属性,如果需要创建多个类似组件,则使用工厂方法,并且仅
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} } }