【发布时间】:2016-01-26 15:55:45
【问题描述】:
很抱歉,除了举个例子,我不知道如何用另一种方式表达我的问题:
public interface IStuff<GenericParameter>{}
public interface IWorkWithStuff<GenericParameter>
{
void doSomethingWithStuff(IStuff<GenericParameter> stuff);
}
public interface IBoth<GenericParameter>
extends IStuff<GenericParameter>, IWorkWithStuff<GenericParameter>
{}
public class Test<Both extends IBoth<?>>
{
Both _myBoth;
void test(final Both otherBoth)
{
_myBoth.doSomethingWithStuff(otherBoth);
}
}
这不编译,有人可以解释为什么吗? 错误是:
IWorkWithStuff 类型中的方法 doSomethingWithStuff(IStuff) 不适用于参数(Both)
另一方面,如果我为参数命名,它会起作用:
public class Test<NamedParameter, Both extends IBoth<NamedParameter>>
{
Both _myBoth;
void test(final Both otherBoth)
{
_myBoth.doSomethingWithStuff(otherBoth);
}
}
这似乎与我非常相似(除了第二种解决方案在我遇到此问题的实际情况下对我来说不可行),有人可以解释这有何不同?
非常感谢!
我补充说我用 Java 1.6 和 Java 1.8 测试过
编辑
awsome 的回答给了我解决方案。
在the link he pointed 中有一个部分名称"Capture helpers" 解释了避免此类问题的方法。
就我而言,这段代码有效:
public class WorkingTest<Both extends IBoth<?>>
{
Both _myBoth;
void test(final Both otherBoth)
{
final IBoth<?> myBoth = _myBoth;
final IBoth<?> _otherBoth = otherBoth;
rebox(myBoth, _otherBoth);
}
protected <Something, SomethingElse> void rebox(final IBoth<Something> both, final IBoth<SomethingElse> otherBoth)
{
both.doSomethingWithStuff(both);
}
}
当类型有效时有效,当类型无效时失败。
谢谢!
编辑
糟糕,我的“解决方案”有一个错误:
我写的
both.doSomethingWithStuff(both);
而不是
both.doSomethingWithStuff(otherBoth);
这不起作用(并且有意义)。
我现在找到的唯一解决方案是使用 cast :
public class WorkingTest<Both extends IBoth<?>>
{
Both _myBoth;
public WorkingTest(final Both myBoth)
{
_myBoth = myBoth;
}
void test(final Both otherBoth)
{
deboxrebox(_myBoth, otherBoth);
}
@SuppressWarnings("unchecked")
protected <CommonParent> void deboxrebox(final Both first, final Both second)
{
final IBoth<CommonParent> _first = (IBoth<CommonParent>) first;
final IBoth<CommonParent> _second = (IBoth<CommonParent>) second;
_first.doSomethingWithStuff(_second);
}
}
至少,它封装了演员阵容,但仍然不是很令人满意。
您认为使用“捕获助手”可以找到更好的解决方案吗?
【问题讨论】:
-
Test
> 什么是Both? -
@rajuGT 貌似是泛型参数的名称;请注意,
Test本身就是一个泛型类型。
标签: java generics type-erasure