我有一些使用Java泛型的类,在我向类层次结构中添加了一些附加层之前,一切工作正常。

我想知道问题是否与“类型擦除”有关,但是我不确定如何表达继承来消除这种情况。

类定义:

public interface IBaseDAO <T, PK extends Serializable>;
public interface IEntityDAO<T extends BaseEntity> extends IBaseDAO<T, Integer>;
public interface IBaseFileDAO<T extends File> extends IEntityDAO<T>;
public interface IInvoiceDAO extends IBaseFileDAO<Invoice>;

public class BaseDAO<T, PK extends Serializable>  implements IBaseDAO<T, PK>;
public abstract class EntityDAO<T extends BaseEntity> extends BaseDAO<T, Integer> implements IEntityDAO<T>;
public abstract class BaseFileDAO<T extends File> extends EntityDAO<T> implements IBaseFileDAO<T>;
public class InvoiceDAO extends BaseFileDAO<Invoice> implements IInvoiceDAO;

public abstract class BaseEntity implements Serializable;
public class File extends BaseEntity;
public abstract class Transaction extends File;
public class Request extends Transaction;
public class Invoice extends Request;


错误是:

Bound mismatch: The type Invoice is not a valid substitute for the bounded parameter <T extends File> of the type BaseFileDAO<T>
Bound mismatch: The type Invoice is not a valid substitute for the bounded parameter <T extends File> of the type IBaseFileDAO<T>


我在这里有点不明白,有人可以给我一些关于如何表达Invoice类以消除错误的建议吗?

编辑:

不知道这是否有帮助,但我也有:

public class FileDAO extends BaseFileDAO<File> implements IFileDAO;
public interface IFileDAO extends IBaseFileDAO<File>;

最佳答案

我想您已经把您的问题弄糟了...这对我来说是编译的:

import java.io.Serializable;

interface IBaseDAO <T, PK extends Serializable> { }
interface IEntityDAO<T extends BaseEntity> extends IBaseDAO<T, Integer> { }
interface IBaseFileDAO<T extends File> extends IEntityDAO<T> { }
interface IInvoiceDAO extends IBaseFileDAO<Invoice> { }

class BaseDAO<T, PK extends Serializable>  implements IBaseDAO<T, PK> { }
abstract class EntityDAO<T extends BaseEntity> extends BaseDAO<T, Integer> implements IEntityDAO<T> { }
abstract class BaseFileDAO<T extends File> extends EntityDAO<T> implements IBaseFileDAO<T> { }
class InvoiceDAO extends BaseFileDAO<Invoice> implements IInvoiceDAO { }

abstract class BaseEntity implements Serializable { }
class File extends BaseEntity { }
abstract class Transaction extends File { }
class Request extends Transaction { }
class Invoice extends Request { }


这里[在提供的代码中]与类型擦除无关。

08-26 07:34