我是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。答案看起來有點奇怪,但它確實有幫助! –