繁体   English   中英

Java泛型和“绑定不匹配”

[英]Java generics and “Bound mismatch”

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM