Java泛型和“绑定不匹配”

时间:2011-11-11 21:27:35

标签: java generics

我有一些使用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>;

1 个答案:

答案 0 :(得分:0)

我认为你已经搞砸了你的问题......这为我编译:

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 { }

这里没有与类型擦除有关[在代码中]。