我是Java新手,需要在Java6中编写通用方法。我的目的可以用下面的C#代码来表示。有人能告诉我如何在Java中编写它?如何在Java中编写通用方法
class Program
{
static void Main(string[] args)
{
DataService svc = new DataService();
IList<Deposit> list = svc.GetList<Deposit, DepositParam, DepositParamList>();
}
}
class Deposit { ... }
class DepositParam { ... }
class DepositParamList { ... }
class DataService
{
public IList<T> GetList<T, K, P>()
{
// build an xml string according to the given types, methods and properties
string request = BuildRequestXml(typeof(T), typeof(K), typeof(P));
// invoke the remote service and get the xml result
string response = Invoke(request);
// deserialize the xml to the object
return Deserialize<T>(response);
}
...
}
无需在Deserialize中使用(我刚刚用JDK 6检查过) –
谢谢你们,Affe和zaske。答案看起来有点奇怪,但它确实有帮助! –