是否可以在 Java 中混合组合和继承?拳头我有一些泛型类是一个HAS-A(或HAS-MANY)关系(Composition)。
通用类:
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 等。
每个特殊结构都有一个特定的 sub-TypeA、sub-TypeB 和 sub-TypeC 等。泛型类定义了一些我希望在处理时重用的代码(属性和方法)具有特殊结构。下面的代码最好地解释了我面临的问题。(我不知道这是否可以通过 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;
}