我从另一个答案中借用了代码
interface IConverter<TFrom, TTo>
{
TTo convert(TFrom from);
}
class IntToStringConverter implements IConverter<Integer, String>
{
public String convert(Integer from)
{
return "This is a string: " + from.toString();
}
}
class ConverterUser<TConverter extends IConverter<TFrom, TTo>, TFrom, TTo>
{
public ConverterUser()
{
}
private List<TConverter> _converter2;
private TConverter _converter;
public void replaceConverter(TConverter converter)
{
_converter = converter;
}
public TTo convert(TFrom from)
{
return _converter.convert(from);
}
}
class Test
{
public static void main(String[] args)
{
ConverterUser<IntToStringConverter, Integer, String> converterUser =
new ConverterUser<IntToStringConverter, Integer, String>();
converterUser.replaceConverter(new IntToStringConverter());
System.out.println(converterUser.convert(328));
}
}
我的问题与这段代码有关
ConverterUser<IntToStringConverter, Integer, String> converterUser =
new ConverterUser<IntToStringConverter, Integer, String>();
正如IntToStringConverter
实现IConverter<Integer, String>
和类型是已知的Integer
,String
java没有推理系统来避免打字new ConverterUser<IntToStringConverter, Integer, String>()
和打字new ConverterUser<IntToStringConverter>()
吗?