英文:
Extract Java Type from generic object
问题
我正在创建一个类,其中有许多方法与片段中的getCategories()方法类似。有没有办法创建一个方法,在下一个操作中动态获取我需要的Type对象,而不是为其他类似的方法手动输入和重复相似的代码。
...
public static void getCategories(CategoriesCallback<List<Category>> callback) {
// 需要创建一个方法从回调对象中获取Type,而不是手动输入它
Type type = new TypeToken<List<Category>>() {}.getType();
// 需要回调和Type
performOperation(callback, type);
}
// 例如
public static void getBooks(BooksCallback<List<Book>> callback) {
// 需要创建一个方法从回调对象中获取Type,而不是手动输入它
Type type = new TypeToken<List<Book>>() {}.getType();
// 需要回调和Type
performOperation(callback, type);
}
...
英文:
I am creating a class with a lot of methods that are similar to the getCategories() method in the snippet. Is there a way to make a method that gets the Type object that i need for the next operation dynamically instead of typing and repeating myself for other similar methods.
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.List;
...
public static void getCategories(CategoriesCallback<List<Category>> callback) {
//Need to create a method to get Type from the callback object instead of manually typing it
Type type = new TypeToken<List<Category>>() {}.getType();
//Requires Callback and Type
performOperation(callback, type);
}
//e.g.
public static void getBooks(BooksCallback<List<Book>> callback) {
//Need to create a method to get Type from the callback object instead of manually typing it
Type type = new TypeToken<List<Book>>() {}.getType();
//Requires Callback and Type
performOperation(callback, type);
}
...
答案1
得分: 0
是的,可以开发一个基础的通用类,由CategoriesCallback和BooksCallback进行扩展。您将会得到类似这样的内容:
public class BaseCallback<T> {
}
这些是那些类:
```java
public class BooksCallback<Book> extends BaseCallback<List<Book>>{
}
public class CategoriesCallback<Category> extends BaseCallback<List<Category>>{
}
现在您可以拥有自己的基础通用方法,如下所示:
```java
public static void getCallbacks(BaseCallback<List<?>> callback) {
英文:
Yes, it's possible to develop a base generic class that are extended by CategoriesCallback and BooksCallback. You will have something like this
public class BaseCallback<T> {
}
And these are those classes:
public class BooksCallback<Book> extends BaseCallback<List<Book>>{
}
public class CategoriesCallback<Category> extends BaseCallback<List<Category>>{
}
And now you can have your own base general method as this:
public static void getCallbacks(BaseCallback<List<?>> callback) {
}
答案2
得分: 0
> 需要创建一个方法,从回调对象中获取类型,而不是手动输入类型。
这是一个常见问题。唯一的方法是使用一个不太正规的技巧,由于Java使用了类型擦除,这被认为是一种不太正式的方式。集合在运行时并不知道它所持有的类型,因为这些信息只在编译时可用。
如果你确实真的需要这个类型,我会将它作为参数传递给你的方法:
public static <T> void getCategories(CategoriesCallback<List<T>> callbackList,
Class<T> clazz) {
然后你可以这样调用:
List<Foo> callbackList = ...
getCategories(callbackList, Foo.class);
英文:
> Need to create a method to get Type from the callback object instead of manually typing it
This is a FAQ. The only way to do this is a hack and is considered back form due to Java using type erasure. The collection does not know what type is holding since that information is only available at compilation time.
If you really and truly need the type then I would pass it into your method:
public static <T> void getCategories(CategoriesCallback<List<T>> callbackList,
Class<T> clazz) {
Then you would call it like:
List<Foo> callbackList = ...
getCategories(callbackList, Foo.class);
专注分享java语言的经验与见解,让所有开发者获益!
评论