绕过糟糕的设计实践:Java多重继承

绕过糟糕的设计实践:Java多重继承,java,inheritance,javafx,Java,Inheritance,Javafx,我正在使用JavaFXShape子类,我遇到了一个我认为相当奇怪的问题。我的目标是扩展其中几个形状子类(即矩形,圆形),以便向这些对象添加我自己的属性。例如,Rectangle子类的扩展如下所示: public class MyRectangle extends javafx.scene.shape.Rectangle implements SpecialInterface { private SpecialAttributes specialAttributes;

我正在使用JavaFX
Shape
子类,我遇到了一个我认为相当奇怪的问题。我的目标是扩展其中几个形状子类(即
矩形
圆形
),以便向这些对象添加我自己的属性。例如,
Rectangle
子类的扩展如下所示:

public class MyRectangle extends javafx.scene.shape.Rectangle 
    implements SpecialInterface {

    private SpecialAttributes specialAttributes;

    // ...
    // Constructors, getters and setters here
    // ...
}
class ManipulationService<T extends Shape & SpecialInterface> {
    public ManipulationService(T myExtendedShape) {
        // method from Shape
        myExtendedShape.onRotate(/* ... */);

        // method from SpecialInterface
        myExtendedShape.getSpecialAttributes();
    }
}
其中,
SpecialInterface
可用于指定与将添加到
MyRectangle
MyCircle
的新属性相关的方法,在这种情况下:

public interface SpecialInterface {

    public SpecialAttributes getSpecialAttributes();
    public void setSpecialAttributes();

} 
然而,当我试图创建引用
Rectangle
Circle
的这些子类的服务类时,似乎我一般无法这样做。本质上,当我需要利用
Shape
子类和
SpecialInterface
接口中的属性和方法时,问题就会出现:

public class ManipulationService{

    public ManipulationService(<Undefined> myExtendedShape) {
        // object from JavaFX Node, inherited by JavaFX Shapes (Circle, Rectangle, etc)
        myExtendedShape.onRotate(new EventHandler<>(){
            // ...
        });

        // a method from MyRectangle or MyCircle
        myExtendedShape.getSpecialAttributes();
    }

    // ...
}
公共类操作服务{
公共操纵服务(myExtendedShape){
//来自JavaFX节点的对象,由JavaFX形状(圆形、矩形等)继承
onRotate(新的EventHandler()){
// ...
});
//来自MyRectangle或MyCircle的方法
myExtendedShape.getSpecialAttributes();
}
// ...
}
这里的问题是,我无法为我的扩展形状创建一个超类来替换上面的
。具体地说,如果我创建一个超类,由于缺乏多重继承,我无法扩展我想在子类中扩展的特定形状。但是,如果我将
替换为
Shape
,那么我将无法访问
SpecialInterface
中的方法


我确信这种多重继承问题以前已经解决了,但我找不到解决办法。我非常感谢所有关于如何处理这种情况的建议。

您可以这样定义
操作服务

public class MyRectangle extends javafx.scene.shape.Rectangle 
    implements SpecialInterface {

    private SpecialAttributes specialAttributes;

    // ...
    // Constructors, getters and setters here
    // ...
}
class ManipulationService<T extends Shape & SpecialInterface> {
    public ManipulationService(T myExtendedShape) {
        // method from Shape
        myExtendedShape.onRotate(/* ... */);

        // method from SpecialInterface
        myExtendedShape.getSpecialAttributes();
    }
}
类操作服务{
公共操纵服务(T myExtendedShape){
//从形状开始的方法
myExtendedShape.onRotate(/*…*/);
//方法从SpecialLinterface开始
myExtendedShape.getSpecialAttributes();
}
}

您可以这样定义
操作服务

public class MyRectangle extends javafx.scene.shape.Rectangle 
    implements SpecialInterface {

    private SpecialAttributes specialAttributes;

    // ...
    // Constructors, getters and setters here
    // ...
}
class ManipulationService<T extends Shape & SpecialInterface> {
    public ManipulationService(T myExtendedShape) {
        // method from Shape
        myExtendedShape.onRotate(/* ... */);

        // method from SpecialInterface
        myExtendedShape.getSpecialAttributes();
    }
}
类操作服务{
公共操纵服务(T myExtendedShape){
//从形状开始的方法
myExtendedShape.onRotate(/*…*/);
//方法从SpecialLinterface开始
myExtendedShape.getSpecialAttributes();
}
}

您可以这样定义
操作服务

public class MyRectangle extends javafx.scene.shape.Rectangle 
    implements SpecialInterface {

    private SpecialAttributes specialAttributes;

    // ...
    // Constructors, getters and setters here
    // ...
}
class ManipulationService<T extends Shape & SpecialInterface> {
    public ManipulationService(T myExtendedShape) {
        // method from Shape
        myExtendedShape.onRotate(/* ... */);

        // method from SpecialInterface
        myExtendedShape.getSpecialAttributes();
    }
}
类操作服务{
公共操纵服务(T myExtendedShape){
//从形状开始的方法
myExtendedShape.onRotate(/*…*/);
//方法从SpecialLinterface开始
myExtendedShape.getSpecialAttributes();
}
}

您可以这样定义
操作服务

public class MyRectangle extends javafx.scene.shape.Rectangle 
    implements SpecialInterface {

    private SpecialAttributes specialAttributes;

    // ...
    // Constructors, getters and setters here
    // ...
}
class ManipulationService<T extends Shape & SpecialInterface> {
    public ManipulationService(T myExtendedShape) {
        // method from Shape
        myExtendedShape.onRotate(/* ... */);

        // method from SpecialInterface
        myExtendedShape.getSpecialAttributes();
    }
}
类操作服务{
公共操纵服务(T myExtendedShape){
//从形状开始的方法
myExtendedShape.onRotate(/*…*/);
//方法从SpecialLinterface开始
myExtendedShape.getSpecialAttributes();
}
}

我想你应该更清楚一点,为什么你提到的类不适用。为什么不创建您自己的超级类型(MyShape)并使用它,以及您需要定义的所有方法?@markspace那么您将如何定义
MyRectangle
?它必须同时从
MyShape
Rectangle
继承,这是不可能的。这是不可能的。我认为这是一个你认为必须有一个解决方案,但可能有其他更好的方法。根据形状定义MyCircleRectangle,并向我们说明为什么它不起作用。@markspace建议您创建一个抽象超类来表示层次结构的分支。这实际上是一个很好的建议。我想你应该更清楚地知道为什么你提到的课程不适用。为什么不创建您自己的超级类型(MyShape)并使用它,以及您需要定义的所有方法?@markspace那么您将如何定义
MyRectangle
?它必须同时从
MyShape
Rectangle
继承,这是不可能的。这是不可能的。我认为这是一个你认为必须有一个解决方案,但可能有其他更好的方法。根据形状定义MyCircleRectangle,并向我们说明为什么它不起作用。@markspace建议您创建一个抽象超类来表示层次结构的分支。这实际上是一个很好的建议。我想你应该更清楚地知道为什么你提到的课程不适用。为什么不创建您自己的超级类型(MyShape)并使用它,以及您需要定义的所有方法?@markspace那么您将如何定义
MyRectangle
?它必须同时从
MyShape
Rectangle
继承,这是不可能的。这是不可能的。我认为这是一个你认为必须有一个解决方案,但可能有其他更好的方法。根据形状定义MyCircleRectangle,并向我们说明为什么它不起作用。@markspace建议您创建一个抽象超类来表示层次结构的分支。这实际上是一个很好的建议。我想你应该更清楚地知道为什么你提到的课程不适用。为什么不创建您自己的超级类型(MyShape)并使用它,以及您需要定义的所有方法?@markspace那么您将如何定义
MyRectangle
?它必须同时从
MyShape
Rectangle
继承,这是不可能的。这是不可能的。我认为这是一个你认为必须有一个解决方案,但可能有其他更好的方法。根据形状定义MyCircleRectangle,并告诉我们为什么它不起作用。@mar