gpt4 book ai didi

Java泛型和 "Bound mismatch"

转载 作者:行者123 更新时间:2023-12-01 05:36:32 30 4
gpt4 key购买 nike

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

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

关于Java泛型和 "Bound mismatch",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8100175/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com