想象以下用 C# 编写的代码:
public class Provider<T>
{
T GetValue(); // implementation skipped for brevity
}
//
public class Converter<T1, T2>
{
T2 Convert(T1 value);// implementation skipped for brevity
}
//
public class SomeClass // No Generics Here
{
public T2 DoSomething<T1, T2>()
{
T1 value = new Provider<T1>().GetValue();
T2 result = new Converter<T1, T2>().Convert(value);
return result;
}
}
// and the usage
...
SomeClass some = new SomeClass();
SomeOtherType result = some.DoSomething<SomeType, SomeOtherType>();
是否可以使用 Java 实现相同的功能 - 我想知道如何通过在方法使用中提供类型参数来调用 Java 中的方法,如上所示。我已经在 .NET 中完成了这项工作,并且我知道 Java 支持类型约束和推理,我只是把它的语法弄乱了。