【发布时间】:2015-03-17 18:04:49
【问题描述】:
有如下结构
我自己的课程
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。
那么将我的超类转换为子类需要什么?
【问题讨论】:
标签: java oop generics inheritance