我有以下類別:獲得「不適用」與泛型類型編譯錯誤
public interface IEntity<T extends IEntity<T>>
{
T createNewEntity(IEntityCreator creator);
}
public interface IEntityCreator
{
<T extends IEntity<T>> T createNewEntity(T source);
}
public abstract class EntityBase<T extends EntityBase<T>> implements IEntity<T>
{
@Override
public T createNewEntity(IEntityCreator creator)
{
return creator.createNewEntity(this); //Error here
}
}
我收到以下錯誤:
「約束不匹配:類型IEntityCreator的一般方法createNewEntity(T)不適用於參數(EntityBase)。推斷的類型EntityBase不是有效的替代參數>「
我嘗試了以下代碼的不同方法,但沒有成功:
public interface IEntity<T extends IEntity<T>>
{
T createNewEntity(IEntityCreator<T> creator);
}
public interface IEntityCreator<T extends IEntity<T>>
{
T createNewEntity(T source);
}
public abstract class EntityBase<T extends EntityBase<T>> implements IEntity<T>
{
@Override
public T createNewEntity(IEntityCreator<T> creator)
{
return creator.createNewEntity(this); //Error here
}
}
這一次的編譯器說:
「在類型IEntityCreator的方法createNewEntity(T)是不適用的參數(EntityBase)」
,這是什麼錯誤的原因是什麼?我該如何糾正它?
感謝
你的第一行:公共接口IEntity> 。在IEntity
2014-09-26 09:06:29
嘗試在'EntityBase.createNewEntity'聲明中替換'EntityBase' –
talex
2014-09-26 09:13:47
如何用'return creator.createNewEntity((T)this'')替換? – 2014-09-26 09:14:37