不同类型参数的方法重载错误
Method Over Loading error on different type arguments
在 android 做我的项目时,我遇到了这个奇怪的问题(可能是我对 java 中的 generics 缺乏理解)方法重载。我在实用程序 class 中定义了以下静态方法,但它给了我错误。
public static void getAllFromDatabase(Context context, ArrayList<Student> students) {
DatabaseHelper dh = new DatabaseHelper(context);
students = dh.getStudents();
dh.close();
}
public static void getAllFromDatabase(Context context, ArrayList<LogEntry> logs) {
DatabaseHelper dh = new DatabaseHelper(context);
logs = dh.getlog();
dh.close();
}
有什么帮助吗?提前致谢。
Remember to put the type parameter in the class definition.
public class CustomCallBack<T> implements Callback {
public void getAllFromDatabase(Context context, ArrayList<T> logs) {
}
}
您可以在 https://docs.oracle.com/javase/tutorial/java/generics/types.html
上引用它
由于类型擦除过程,您遇到此问题是因为之后两种方法都相同。
来自Doc
During the type erasure process, the Java compiler erases all type
parameters and replaces each with its first bound if the type
parameter is bounded, or Object if the type parameter is unbounded.
见Java generics - type erasure - when and what happens
这是因为
当 Java 实现泛型时,为了使字节码向后兼容,java 擦除类型以使签名成为泛型,在运行时泛型信息消失并且签名看起来像:
public static void getAllFromDatabase(Context context, ArrayList students);
public static void getAllFromDatabase(Context context, ArrayList logs);
所以你有两个参数类型相同的方法,因此你会得到一个错误。
尝试更多;
http://docs.oracle.com/javase/tutorial/java/generics/erasure.html
估计是因为擦除;如果 Java 从一开始就有泛型,这可能就不会发生。您可能会看到这两个参数是不同的类型,但是当 <> 被移除时,JVM 将看到两个方法的参数类型为 Map。
不过,您可以通过为方法提供不同的 return 类型来作弊。虽然擦除后它们都具有相同的名称和参数,但在字节码上会有所不同,因为整个签名不一样——return 类型不同
由于 Type Erasure,您收到错误消息。类型擦除在编译时删除所有泛型信息。
方法解析发生在编译时,不考虑类型参数。
Java 泛型使用类型擦除。尖括号 <Student>
和 <LogEntry>
中的位被删除,因此您最终会得到两个具有相同签名的方法。这是不允许的,因为运行时不知道每种情况使用哪个。
在 android 做我的项目时,我遇到了这个奇怪的问题(可能是我对 java 中的 generics 缺乏理解)方法重载。我在实用程序 class 中定义了以下静态方法,但它给了我错误。
public static void getAllFromDatabase(Context context, ArrayList<Student> students) {
DatabaseHelper dh = new DatabaseHelper(context);
students = dh.getStudents();
dh.close();
}
public static void getAllFromDatabase(Context context, ArrayList<LogEntry> logs) {
DatabaseHelper dh = new DatabaseHelper(context);
logs = dh.getlog();
dh.close();
}
有什么帮助吗?提前致谢。
Remember to put the type parameter in the class definition.
public class CustomCallBack<T> implements Callback {
public void getAllFromDatabase(Context context, ArrayList<T> logs) {
}
}
您可以在 https://docs.oracle.com/javase/tutorial/java/generics/types.html
上引用它由于类型擦除过程,您遇到此问题是因为之后两种方法都相同。
来自Doc
During the type erasure process, the Java compiler erases all type parameters and replaces each with its first bound if the type parameter is bounded, or Object if the type parameter is unbounded.
见Java generics - type erasure - when and what happens
这是因为 当 Java 实现泛型时,为了使字节码向后兼容,java 擦除类型以使签名成为泛型,在运行时泛型信息消失并且签名看起来像:
public static void getAllFromDatabase(Context context, ArrayList students);
public static void getAllFromDatabase(Context context, ArrayList logs);
所以你有两个参数类型相同的方法,因此你会得到一个错误。 尝试更多; http://docs.oracle.com/javase/tutorial/java/generics/erasure.html
估计是因为擦除;如果 Java 从一开始就有泛型,这可能就不会发生。您可能会看到这两个参数是不同的类型,但是当 <> 被移除时,JVM 将看到两个方法的参数类型为 Map。
不过,您可以通过为方法提供不同的 return 类型来作弊。虽然擦除后它们都具有相同的名称和参数,但在字节码上会有所不同,因为整个签名不一样——return 类型不同
由于 Type Erasure,您收到错误消息。类型擦除在编译时删除所有泛型信息。
方法解析发生在编译时,不考虑类型参数。
Java 泛型使用类型擦除。尖括号 <Student>
和 <LogEntry>
中的位被删除,因此您最终会得到两个具有相同签名的方法。这是不允许的,因为运行时不知道每种情况使用哪个。