Java泛型:创建一个在父级上运行的通用方法类
Java Generics: create a general method operating on parent classes
有如下结构
我自己的类
class HERMEntityRelationshipType
class HERMEntityType extends HERMEntityRelationshipType
class HERMRelationshipType extends HERMEntityRelationshipType
类 从框架生成。
class DBMEntityRelationshipType extends DBMDataObject
class DBMEntityType extends DBMDataObject
class DBMRelationshipType extends DBMDataObject
我写了两个类似的方法
private HERMEntityType parseERType(DBMEntityType dbmEntityType) {...}
private HERMRelationshipType parseERType(DBMRelationshipType dbmRelationshipType){...}
但我只想有这样一种方法:
HERMEntityRelationshipType parseERType(DBMEntityRelationshipType dbmERType){...}
但是在调用该通用方法后我无法将我的 类 转换为子类:例如 HERMEntityRelationshipType
到 HERMEntityType
。但是将 DBMDataObject
转换为 DBMEntityRelationshipType
效果很好。所以他们必须比我更聪明地实施这些 类。我的演员阵容看起来像这样:
HERMEntityType entityType = (HERMEntityType) parseERType((DBMEntityRelationshipType) dataobject);
结果为:Exception in thread "main" java.lang.ClassCastException: hermtransformation.herm.HERMEntityRelationshipType cannot be cast to hermtransformation.herm.HERMEntityType
.
那么将我的超类转换为子类需要什么?
DBMEntityRelationshipType 和 HERMEntityType 之间似乎没有关系。根据您的输入模型,缺少 DBMDataObject 和 HERMEntityRelationshipType 之间的关系。理想情况下,如果 DBMEntityRelationshipType 也从 HERMEntityRelationshipType 扩展,则此转换将起作用。此外,您需要将父引用强制转换为项目多态性。
这里的问题是 Java 不允许向下转型。您应该创建子 class 的新对象,而不是返回父 class.
的新对象
parseERType 方法应如下所示:
HERMEntityRelationshipType parseERType(DBMEntityRelationshipType dbmERType){
if(dbmERType.getClass().equals(DBMEntityType.class)) {
return new HERMEntityType(dbmERType);
} else {
return new HERMRelationshipType(dbmERType);
}
}
有如下结构
我自己的类
class HERMEntityRelationshipType
class HERMEntityType extends HERMEntityRelationshipType
class HERMRelationshipType extends HERMEntityRelationshipType
类 从框架生成。
class DBMEntityRelationshipType extends DBMDataObject
class DBMEntityType extends DBMDataObject
class DBMRelationshipType extends DBMDataObject
我写了两个类似的方法
private HERMEntityType parseERType(DBMEntityType dbmEntityType) {...}
private HERMRelationshipType parseERType(DBMRelationshipType dbmRelationshipType){...}
但我只想有这样一种方法:
HERMEntityRelationshipType parseERType(DBMEntityRelationshipType dbmERType){...}
但是在调用该通用方法后我无法将我的 类 转换为子类:例如 HERMEntityRelationshipType
到 HERMEntityType
。但是将 DBMDataObject
转换为 DBMEntityRelationshipType
效果很好。所以他们必须比我更聪明地实施这些 类。我的演员阵容看起来像这样:
HERMEntityType entityType = (HERMEntityType) parseERType((DBMEntityRelationshipType) dataobject);
结果为:Exception in thread "main" java.lang.ClassCastException: hermtransformation.herm.HERMEntityRelationshipType cannot be cast to hermtransformation.herm.HERMEntityType
.
那么将我的超类转换为子类需要什么?
DBMEntityRelationshipType 和 HERMEntityType 之间似乎没有关系。根据您的输入模型,缺少 DBMDataObject 和 HERMEntityRelationshipType 之间的关系。理想情况下,如果 DBMEntityRelationshipType 也从 HERMEntityRelationshipType 扩展,则此转换将起作用。此外,您需要将父引用强制转换为项目多态性。
这里的问题是 Java 不允许向下转型。您应该创建子 class 的新对象,而不是返回父 class.
的新对象parseERType 方法应如下所示:
HERMEntityRelationshipType parseERType(DBMEntityRelationshipType dbmERType){
if(dbmERType.getClass().equals(DBMEntityType.class)) {
return new HERMEntityType(dbmERType);
} else {
return new HERMRelationshipType(dbmERType);
}
}