0

我有以下结构:

public abstract class BaseVersionedEntity {
    private long id;

    private List<BaseRevision<? extends BaseVersionedEntity>> versions;

    public BaseRevision<? extends BaseVersionedEntity> getLatestRevision() {
        return versions.get(versions.size() - 1);
    }

    public abstract BaseRevision<? extends BaseVersionedEntity> newRevision();
}

public abstract class BaseVersionedEntityData<E> {
    private long id;

    private BaseRevision<E> revision;
}

public abstract class BaseRevision<E> implements Comparable<BaseRevision<E>> {
    private long id;
    private Timestamp timestamp;

    private E versionedEntity;
    private BaseVersionedEntityData<E> versionedEntityData;

    public BaseVersionedEntityData<E> getVersionedEntityData() {
        return versionedEntityData;
    }
}

这将通过以下方式实现:

public class PersonEntity extends BaseVersionedEntity {

    @Override
    public BaseRevision<? extends BaseVersionedEntity> newRevision() {
        PersonRevision newRevision = new PersonRevision();
        newRevision.setTimestamp(new Timestamp(System.currentTimeMillis()));
        getRevisions().add(newRevision);
        return newRevision;
    }
}

public class PersonData extends BaseVersionedEntityData<PersonEntity> {

}

public class PersonRevision extends BaseRevision<PersonEntity> {

}

在我的代码中的某处,我将执行以下调用:

// is not null
PersonEntity personEntity;

PersonData personData = personEntity.getLatestRevision().getVersionedEntityData();

由于某些原因标记为类型不匹配...

Type mismatch: cannot convert from BaseVersionedEntityData<capture#1-of ? extends BaseVersionedEntity> to PersonData

谁能找到错误??或者有什么提示??

谢谢!!本杰明

4

1 回答 1

1

该方法getLatestRevision不返回 a PersonRevision,它返回 a BaseRevision,即使PersonRevision那样也不返回PersonData- 您需要显式转换,因为这是一个向下转换,甚至不是由类型擦除导致的“安全”向下转换之一:

PersonData personData = (PersonData)(personEntity.getLatestRevision().getVersionedEntityData());
于 2012-12-06T21:52:23.663 回答