java - 在 Java 中混合组合和继承

标签 java inheritance object-composition

Java 中是否可以混合组合和继承?首先,我有一些通用类是 HAS-A(或 HAS-MANY)关系(组合)。

通用类:

Structure、TypeA、TypeB、TypeC 等,其中 Structure 与 TypeA、TypeB 和 TypeC 是 HAS-A 关系。

但是我也有几组子类,它们从泛型类继承(继承)并且具有相同的关系。

子类集:

设置 1:

Structure1、TypeA1、TypeB1、TypeC1 等,其中 Structure1 与 TypeA1、TypeB1 和 TypeC1 具有相同的 HAS-A 关系,就像泛型类一样。

并且:Structure1 扩展 Structure,TypeA1 扩展 TypeA,...,TypeC1 扩展 TypeC,等等

...直到设置 X:

StructureX、TypeAX、TypeBX、TypeCX 等

每个特殊结构都有一个特定的子类型A、子类型B和子类型C等。通用类定义了一些我希望在处理特殊结构时重用的代码(属性和方法)。 下面的代码可以最好地解释我面临的问题。 (我不知道这是否可以通过 Java“泛型”以某种方式解决,但我认为不能。)

/***********************************************************************************************
/ Generic Structure of Structure-Class with Instances of Generic other classes (Composition)
/***********************************************************************************************/
class Structure {

    // Substructures
    TypeA instanceA;
    TypeB instanceB;

    // Defining many methods using the instances of other generic classes like TypeA
    void setGenericAttributeOfA(int value) {
        instanceA.genericAttribute = value;
    }
    void setGenericAttributeOfB(int value) {
        instanceB.genericAttribute = value;
    }
}

class TypeA {
    int genericAttribute;
}
class TypeB {
    int genericAttribute;
}

/***********************************************************************************************
/ Specific implementation of a Structure-Class with specific implementation of the other classes (Inheritance)
/***********************************************************************************************/

// In the specific implementations I want to use the generic methods, because I do not want to 
// rewrite the code for each and every specific implementation. But they should 

class Structure1 extends Structure {

    // This will create an additional attribute instanceA of specific TypeA1, so I will end up with two instances:
    // (1) TypeA super.instanceA and (2) TypeA1 this.instanceA. But what I would like is to have only
    // one instanceA of type TypeA1 that can also be used by the global methods of the generic Structure.
    TypeA1 instanceA;

    Structure1() {
        // This creates an instance of type TypeA1, but it cannot be used in the generic methods
        // because it is hold in a separate "local" variable that is not known to the generic Structure methods
        instanceA = new TypeA1();
        // This creates an instance of type TypeB1, but it cannot access the "local" specific attributes,
        // because it is hold in a varable which is statically types as TypeB
        instanceB = new TypeB1(); 
    }

    void specificMethod() {
        setGenericAttributeOfA(42); // would fail, because instanceA of type generic TypeA is null
        instanceA.specificAttribute = 13; // works only for "local" specific attributes

        setGenericAttributeOfB(42); // works only for generic attributes
        instanceB.specificAttribute = 13; // would fail, because instanceB is statically typed as generic TypeB which does not have this attribute
        ((TypeB1)instanceB).specificAttribute = 13; // works but is an ugly work-around and over-complicated if to be used many times
    }
}

class TypeA1 extends TypeA {
    int specificAttribute;
}
class TypeB1 extends TypeB {
    int specificAttribute;
}

最佳答案

也许Generics可以帮忙吗?

结构声明为通用:

class Structure<T1 extends TypeA, T2 extends TypeB, T3 extends TypeC> {
    T1 instanceA;
    T2 instanceB;
    T3 instanceC;
}

并且您的特定结构将通过指定类型参数从通用 Structure 继承:

class Structure1 extends Structure<TypeA1, TypeB1, TypeC1> {
    // here instanceA will be of type TypeA1, instanceB will be TypeB1 etc.
}

关于java - 在 Java 中混合组合和继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50048195/

相关文章:

javascript - 如果访问 anchor 标记,则设置其父元素的样式

c# - C#中具有静态属性的抽象类的继承

go - 在golang的继承方法中使对象可访问

java - 为什么有时需要将 JAR 添加到构建路径而有时不需要?

Java 多线程矩阵乘法

C++ 避免向下转型

python - 实现表示 "a list with a title"的类的 pythonic 方法是什么?

java - 高效等待资源

java - 守护进程线程数、线程数和总启动线程数