【问题标题】:How to extract a parent class from sub-classes that share very similar logic?如何从具有非常相似逻辑的子类中提取父类?
【发布时间】:2019-02-08 04:29:35
【问题描述】:

我正在尝试创建几个最终可以添加到 JToolBar 的自定义按钮。我有一个从 JButton 扩展而来的类,看起来像这样:

public class CustomToolbarJButton extends JButton {
    public void setCustomProperties() {
        this.putClientProperty("Property1", "Value1");
    }

    // Some other CustomToolbarJButton specific code here.
}

我有另一个类,CustomToolbarJToggleButton,它从 JToggleButton 扩展而来,并具有与 setCustomProperties() 方法完全相同的代码。

我的问题是,有什么方法可以创建一个抽象父类,这两个类最终可以继承自,这样我就可以将 setCustomProperties() 方法拉到该父类。

编辑

我想为我最终想要做的事情添加一些背景信息。

我想要一个这样的父类:

public abstract class CustomToolbarButton extends <some-class> {
    public void setCustomProperties() {
        this.putClientProperty("Property1", "Value1");
    }
}

public class CustomToolbarJButton extends CustomToolbarButton {
    // Some other CustomToolbarJButton specific code here.
}

public class CustomToolbarJToggleButton extends CustomToolbarButton {
    // Some other CustomToolbarJToggleButton specific code here.
}

并最终将按钮添加到工具栏,我想创建一个类似的方法:

public void addCustomButtonToToolbar(boolean standardButtonOrToggleButton, String text) {
    CustomToolbarButton customToolbarButton = standardButtonOrToggleButton ? new CustomToolbarJButton(text) : new CustomToolbarJToggleButton(text);
    customToolbarButton.setCustomProperties();
    this.toolbar.add(customToolbarButton);  // toolbar is a JToolBar. I wanted to add the customToolbarButton directly to it, just like a standard JComponent.
}

这样的事情可能吗?

【问题讨论】:

  • 由于java不支持多重继承,我建议你使用带有setCustomProperties()方法的接口,或者你必须在CustomToolbarJButton类和JButton之间有抽象类类
  • 如果CustomToolbarJButtonCustomToolbarJToggleButton 具有完全相同的属性,即相同的名称和相同的值,请编写一个单独的方法来接受任一实例并设置属性。可能是单独类中的静态方法。

标签: java inheritance


【解决方案1】:

是的,您可以使用interface 来做到这一点,它与abstract class 非常相似,但只有方法,没有字段:

public interface CustomProperties {
    public void setCustomProperties();
}

和:

public class CustomToolbarJButton extends JButton implements CustomProperties {
    public void setCustomProperties() {
        this.putClientProperty("Property1", "Value1");
    }

    // Some other CustomToolbarJButton specific code here.
}

注意implements关键字,你可以同时从多个接口继承..

我认为您可能需要它,您可以使用子类创建 CustomProperties 类型的对象。

CustomProperties obj = (CustomProperties) new CustomToolbarJButton();

希望对你有帮助:)

编辑:这可能会有所帮助,可能您有不同的要求..

public interface CustomProperties {
    public void setCustomProperties() {
        this.putClientProperty("Property1", "Value1");
    }
}

.

public class CustomToolbarJButton extends JButton implements CustomProperties {
    // Some other CustomToolbarJButton specific code here.
}

.

public class CustomToolbarJToggleButton extends JToggleButton implements CustomProperties {
    // Some other CustomToolbarJToggleButton specific code here.
}

这个:编辑:

public void addCustomButtonToToolbar(boolean standardButtonOrToggleButton, String text) {
    CustomProperties customToolbarButton = standardButtonOrToggleButton ? new CustomToolbarJButton(text) : new CustomToolbarJToggleButton(text);
    customToolbarButton.setCustomProperties();
    this.toolbar.add((Component) customToolbarButton);
}

【讨论】:

  • 注意:从 Java 8 开始,您实际上可以将默认方法放入具有方法体的接口中。
  • @AJo 也看看上面的评论 :)
  • 感谢您的回复!如果可能的话,请你看看我对这个问题所做的编辑,给出我最终想要实现的一些背景。
  • @AJo 请看一下这个编辑,我认为这应该适合你
  • @PranshuKhandal 尽管CustomToolbarJButtonCustomToolbarJToggleButtonComponent 的,它们的实例被分配给CustomProperties 这不是Component,因此add() 方法的JToolBar 不能直接在customToolbarButton 实例上调用。
猜你喜欢
  • 1970-01-01
  • 2016-01-21
  • 1970-01-01
  • 2020-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多