如何从共享非常相似的逻辑的子类中提取父类?

时间:2019-02-08 04:29:36

标签: java inheritance

我试图创建几个自定义按钮,最终可以将它们添加到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.
}

这样可以吗?

1 个答案:

答案 0 :(得分:1)

是的,您可以使用interface来做到这一点,abstract classpublic 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 obj = (CustomProperties) new CustomToolbarJButton(); 关键字,您可以同时从多个接口继承。.

正如我认为您可能需要的那样,您可以使用那里的子类创建CustomProperties类型的对象。

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);
}

和这个:编辑:

    @ Table(name = “cluster”, schema = “schema1”)
    @ SecondaryTable(name = “Cluster”, schema = “schema2”, pkJoinColumns = @ PrimaryKeyJoinColumn(name = “clusterId”, referencedColumnName = “objid”))
    @ org.hibernate.annotations.Table(appliesTo = “Cluster”, optional = false)
    @ Entity
    @ OptimisticLocking(type = OptimisticLockType.DIRTY)
    @ DynamicUpdate
    public class Cluster implements Serializable {

    }