【发布时间】:2016-11-07 09:22:10
【问题描述】:
检查我的自定义课程。如果我将 DoSomething() 更改为
final Custom<User> _custom = new Custom<>(new TypeToken<Custom<User>.Response<User>>(){}, "");
那么DEBUG日志就是:"com.application.models.Custom$Response<com.application.models.User>" serverResponse: ""
但如果 DoSomething 是:
final Custom<T> _custom = new Custom<>(new TypeToken<Custom<T>.Response<T>>(){}, "");
调试日志为:
"com.application.models.Custom$Response<T>" serverResponse: "
我有这样的课:
public class Main
{
public static <T> void DoSomething()
{
final Custom<T> _custom = new Custom<>(new TypeToken<Custom<T>.Response<T>>(){}, "");
}
}
//我在变量的右边添加了调试日志
public class Custom<T>
{
private TypeToken<Response<T>> _responseType; _response: null
private Response<T> _response; _response: null
private String _serverResponse; _serverResponse = null;
public Custom(TypeToken<Response<T>> responseType, String serverResponse) `responseType: "com.application.models.Custom$Response<T>" serverResponse: "`
{
this._responseType = responseType;
this._serverResponse = serverResponse;
}
public class Response<t>
{
private List<t> data = null;
public List<t> GetData()
{
return this.data;
}
}
}
这就是我所说的 Main..
public class User
{
public int Id;
public void Test()
{
Main.<User>DoSomething();
}
}
【问题讨论】:
-
你的意思是什么,由于某种原因,当我在创建自定义时编译 T 仍然是通用的?
-
我假设
Main<User> main = new User<>();是一个错字,应该是Main<User> main = new Main<>();,对吧? -
是的,我更新了答案
-
我没有发现任何问题(除了
User类中缺少class关键字和IOnSuccess(List<User> list)中缺少返回类型。哦,您可能想更改new ArrayList()到new ArrayList<>()。 -
很奇怪,我不断收到 com.application.testing.Custom
如果我在自定义中使用 Main 中的“T”
标签: java android generics methods