Android中的java泛型 [英] java generics in Android
问题描述
我不明白以下代码:
public class EventAdapter extends ArrayAdapter< Event>
p>
public EventAdapter(Context context,int textViewResourceId,
List< Event> objects)
{
super(context,textViewResourceId,objects);
this.resource = textViewResourceId;
$ / code $ / pre
我很困惑
在这两种情况下,< Event>
部分。我知道这与泛型有关,但我不明白。我阅读了 http://docs.oracle.com/javase/tutorial/java/generics/ ,但仍然不明白。
我知道
objects
是不明白是用Type
类型的对象的$ ArrayList
< Event>
来扩展ArrayAdapter。这意味着什么?解决方案
extends ArrayAdapter< Event>
这里的类型限制会影响类中方法的返回类型以及方法的参数类型。
下面是一个例子,如果你有一个类:
class SomeClass< T> {
保护T值;
public void setValue(T value){
this.value = value;
}
public T getValue(){
返回值;
$ / code>如果你有另一个类:
class SubClass extends SomeClass {
@Override
public void setValue(Event value){//失败!这并不是压倒超级班的方法。
this.value = value; // 警告!未检查的类型(可能不一致)。
$ b如果你删除了@ @覆盖
注释,它会运行。但是扩展SomeClass
是没用的,如果你把它保留在那里会导致问题 - 会有两个非常相似的方法:setValue(Event) code>和
super.setValue(T)
。现在问题是子类是否可以访问super.setValue(T)
方法?我会在最后解释它,请参阅A缺少类型参数边界示例。
因此,您需要在声明中指定类型:
pre >class SubClass extends SomeClass< Event> {
@Override
public void setValue(Event value){//现在正确!
this.value = value;
$ b $ p
$ b另外,如果声明不一致的类型: p>
class SubClass extends SomeClass< String> {
@Override
public void setValue(Event value){//失败!不压倒一切。
this.value = value; //失败!类型不一致。
所以这个类型限制了类体的行为。 / p>
一个缺少的类型参数边界示例:
import java.lang.reflect。*;
class Super< T> {
public void method(T t){
System.out.println(Hello);
}
public void method2(){
}
}
public class Test extends {
/ * public void method(Object t){
System.out.println(world);
} * /
/ * public< T>无效方法(T t){
} * /
public static void main(String args []){
new Test()。method( ); (方法m:Test.class.getMethods()){
System.out.println(m.toGenericString());
}
}
}
如果我在子类中注释
method()
,它将被编译为一个警告:Test.java使用未选中或不安全的操作 code>。在运行结果中,它将泛型类型
T
变成了Object :
public void Test。方法(java.lang.Object)
。
如果我只取消注释第一个方法()
在它的子类中,它被编译时没有警告。在运行结果中,子类拥有一个public void Test.method(java.lang.Object)
。但是它不允许@Override
注释。 如果我只取消注释第二个method()
在子类中(它也有一个通用类型边界),编译失败并出现错误:名称冲突
。它也不允许@Override
注释。如果这样做,它会抛出一个不同的错误:方法不会覆盖
。
超类中的
method2()
被子类一致继承。但是你也不能写下面的代码:
:
public void method2(Object obj)
和在子类中:public< T> void method2(T obj)
。它们也不明确,编译器也不允许。
I don't understand the following code:
public class EventAdapter extends ArrayAdapter<Event> { public EventAdapter(Context context, int textViewResourceId, List<Event> objects) { super(context, textViewResourceId, objects); this.resource = textViewResourceId; } }
I am confused about the
<Event>
part in both cases. I understand it has something to do with Generics, but I don't understand it. I read http://docs.oracle.com/javase/tutorial/java/generics/, but still don't understand.I do understand that
objects
is anArrayList
of objects of the typeEvent
.The part I don't understand is extending an ArrayAdapter with the Type
<Event>
. What does this signify?解决方案
extends ArrayAdapter<Event>
The type restriction here will influence on the return types of methods in the class, and the argument types of methods.
Here is an example, if you have a class:
class SomeClass<T> { protected T value; public void setValue (T value) { this.value = value; } public T getValue () { return value; } }
And if you have another class:
class SubClass extends SomeClass { @Override public void setValue (Event value) { // Fail! It is not overriding the super class' method. this.value = value; // Warning! Unchecked types (maybe inconsistent). } }
If you remove the
@Override
annotation, it will run. But theextends SomeClass
is useless and might cause problem if you keep it there -- there will be two very similar methods:setValue(Event)
andsuper.setValue(T)
. Now the question is will the subclass have access to thesuper.setValue(T)
method? I will explain it in the end, see "A missing type parameter bounding example".So, you need to specify the type in declaration:
class SubClass extends SomeClass<Event> { @Override public void setValue (Event value) { // Correct now! this.value = value; } }
Also, if you declare an inconsistent type:
class SubClass extends SomeClass<String> { @Override public void setValue (Event value) { // Fail! Not overriding. this.value = value; // Fail! Inconsistent types. } }
So the type restricts the behavior of class body.
A missing type parameter bounding example:
import java.lang.reflect.*; class Super<T> { public void method (T t) { System.out.println("Hello"); } public void method2 () { } } public class Test extends Super { /*public void method (Object t) { System.out.println("world"); }*/ /*public <T> void method (T t) { }*/ public static void main (String args[]) { new Test().method(""); for (Method m : Test.class.getMethods()) { System.out.println(m.toGenericString()); } } }
If I comment
method()
in the subclass, it is compiled with a warning:Test.java uses unchecked or unsafe opertations
. In the running result, it turned the generic typeT
intoObject
:public void Test.method(java.lang.Object)
.If I only uncomment the first
method()
in the subclass, it is compiled with no warnings. In the running result, the subclass owns onepublic void Test.method(java.lang.Object)
. But it doesn't allow@Override
annotation.If I only uncomment the second
method()
in the subclass (which also has a generic type bounding), the compile fails with an error:name clash
. It also doesn't allow@Override
annotation. If you do so, it throws a different error:method does not override
.
method2()
is inherited by the subclass unanimously. But you also can't write the following code:in superclass:
public void method2 (Object obj)
and in subclass:public <T> void method2 (T obj)
. They are also ambiguous and is not allowed by the compiler.这篇关于Android中的java泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!