所以我有一个模特Model.
public class Model { .... }
Run Code Online (Sandbox Code Playgroud)
这有两个子类:
public class SubmodelA extend Model { .... }
Run Code Online (Sandbox Code Playgroud)
和
public class SubmodelB extend Model { .... }
Run Code Online (Sandbox Code Playgroud)
这三个都是在Data课堂上包装的.
public class ApiData<T extends Model> {
public T data;
}
Run Code Online (Sandbox Code Playgroud)
我的将军response wrapper看起来像这样:
public class ApiResponse<DATA> {
DATA data;
}
Run Code Online (Sandbox Code Playgroud)
"虚拟"api操作保持不变:
public interface Endpoints {
Call<ApiResponse<ApiData>> getData();
}
Run Code Online (Sandbox Code Playgroud)
我有一个retrofit2.Callback处理响应的实现:
public class ApiCallbackProxy<T> implements retrofit2.Callback<T> {
public interface ApiResultListener<RESPONSE_TYPE> {
void onResult(RESPONSE_TYPE response, ApiError error);
}
private ApiResultListener<T> …Run Code Online (Sandbox Code Playgroud) 在Java中我可以演员:
List<?> j = null;
List<Integer> j2 = (List<Integer>)j;
Run Code Online (Sandbox Code Playgroud)
那么为什么以下失败呢?
List<List<?>> i = null;
List<List<Integer>> i2 = (List<List<Integer>>)i;
Run Code Online (Sandbox Code Playgroud) 我得到以下编译消息:
[javac] ... error: incompatible types
[javac] exceptionClassHolder = new Holder<>( (new Exception()).getClass() );
[javac] ^
[javac] required: Holder<Class<? extends Exception>>
[javac] found: Holder<Class<CAP#1>>
[javac] where CAP#1 is a fresh type-variable:
[javac] CAP#1 extends Exception from capture of ? extends Exception
[javac] 1 error
Run Code Online (Sandbox Code Playgroud)
在我看来,根据信息所有应该是正确的.CAP#1确实扩展了Exception.那么如何理解上述信息呢?下面的SSCCE(最初没有发布,因为我希望在一般情况下理解错误消息本身):
class Holder<T> {
public T t;
public Holder(T t) {
this.t = t;
}
}
public class FooMain {
public static void main(String args[]) throws Exception {
Holder<Class<? extends Exception>> exceptionClassHolder;
exceptionClassHolder = …Run Code Online (Sandbox Code Playgroud) 我有以下代码成功编译:
import java.lang.String;
import java.util.List;
import java.util.Arrays;
interface Supplier<R> {
Foo<R> get();
}
interface Foo<R> {
public R getBar();
public void init();
}
public class Main {
static private <V> void doSomething(final Supplier<? extends List<? extends V>> supplier) {
// do something
}
static public void main(String[] args) {
doSomething(new Supplier<List<Object>>(){
@Override
public Foo<List<Object>> get() {
return new Foo<List<Object>>(){
@Override
public List<Object> getBar() {
return null;
}
@Override
public void init() {
// initialisation
}
};
}
});
}
} …Run Code Online (Sandbox Code Playgroud) 对于以下代码示例:
public static class Abc<X> { }
public static class Def<Y> { }
public static class Ghi<Z> { }
public void doThis() {
List<?> listOne;
List<Abc<?>> listTwo;
List<Abc<Def<?>>> listThree;
List<Abc<Def<Ghi<?>>>> listFour;
List<Abc<Def<Ghi<String>>>> listFive;
Abc<Def<Ghi<String>>> abcdef;
abcdef = new Abc<Def<Ghi<String>>>();
listOne.add(abcdef); // line 1
listTwo.add(abcdef); // line 2
listThree.add(abcdef); // line 3
listFour.add(abcdef); // line 4
listFive.add(abcdef); // line 5
}
Run Code Online (Sandbox Code Playgroud)
第1,3和4行不编译:
(第1行)
The method add(capture#1-of ?) in the type List<capture#1-of ?> is not applicable for the arguments (Abc<Def<Ghi<String>>>)
Run Code Online (Sandbox Code Playgroud)
(第3行)
The …Run Code Online (Sandbox Code Playgroud) 当我尝试编译以下代码时:
LinkedList<List<? extends Number>> numList = new LinkedList<List<Integer>>();
Run Code Online (Sandbox Code Playgroud)
我得到一个不兼容的类型错误:
Required: LinkedList <java.util.list<? extends java.lang.Number>>
Found: LinkedList <java.util.list<Integer>>
Run Code Online (Sandbox Code Playgroud)
如何实现LinkedList包含具有List扩展元素的元素Number?
为了清楚起见,我希望以numList下列方式添加列表:
numList.add(new LinkedList<Integer>());
考虑类中sort方法的一个重载定义Array:
public static <T> void sort(T[] a, Comparator<? super T> c)
Run Code Online (Sandbox Code Playgroud)
以相反顺序对数组进行排序的常用方法是将Comparator返回的for Collections.reverseOrder()作为第二个参数传递给此方法.
让我们看一下Collections.reverseOrder()openjdk 7 中方法的实现:
public static <T> Comparator<T> reverseOrder() {
return (Comparator<T>) ReverseComparator.REVERSE_ORDER;
}
Run Code Online (Sandbox Code Playgroud)
ReverseComparator 类:
private static class ReverseComparator
implements Comparator<Comparable<Object>>, Serializable {
private static final long serialVersionUID = 7207038068494060240L;
static final ReverseComparator REVERSE_ORDER = new ReverseComparator();
public int compare(Comparable<Object> c1, Comparable<Object> c2) {
return c2.compareTo(c1);
}
private Object readResolve() { return reverseOrder(); }
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:为什么Collections.reverseOrder()要成为通用的?为什么根本 …
考虑我的自定义扩展hashmap:
public class CustomHashMap extends HashMap<String, Object> {
...
}
Run Code Online (Sandbox Code Playgroud)
为什么这不起作用,因为CustomHashMap是HashMap的子代?
Map<String, HashMap<String, Object>> customs = new LinkedHashMap<String, CustomHashMap>();
Run Code Online (Sandbox Code Playgroud)
但这有效:
Map<String, HashMap<String, Object>> customs = new LinkedHashMap();
Run Code Online (Sandbox Code Playgroud)
当将CustomHashMap添加(放入)customsMap 时,它也可以工作.
customs.put("test", new CustomHashMap());
Run Code Online (Sandbox Code Playgroud)
在初始化工作中没有指定泛型似乎很奇怪,但它没有.
我有一个A实现Parcelable 的抽象类.
我有一个班级B和一个班级C都延伸A.我怎样才能使它们变得可以分辨?
因为我可以链接它并在许多帖子中提供CREATORin A和Blike之类的建议.但由于我有其他对象存储A-B-C类并实现Parcelable自己,这种方法似乎不起作用,因为当我想传递一个ArrayList时,A我将不得不CREATOR在类型列表中使用
ArrayList<A> elements = new ArrayList<>();
in.readTypedList(elements , B.CREATOR); // B.CREATOR? C.CREATOR???
Run Code Online (Sandbox Code Playgroud)
这显然没有意义.那么我怎样才能正确制作A Parcelable?
即我想使这个类Parcelable所以我可以以一种常见的方式引用A.
一个)
public abstract class A implements Parcelable {
final String globalVar;
public A(String globalVar) {
this.globalVar = globalVar;
}
}
Run Code Online (Sandbox Code Playgroud)
B)
public class B extends A {
String bVar;
public B(String global, String bVar) {
super(global);
this.bVar = bVar;
}
private B(Parcel …Run Code Online (Sandbox Code Playgroud) 我有一个关于应用多态的问题:让我们假设我有一个类Bird,并且我有许多扩展它的类(比如Pigeon,Falcon依此类推).接下来,我有一Cage堂课.在这堂课中,我想列出一个生活在笼子里的鸟类(只有一种鸟可以生活在每个笼子里).
因此,我不知道列表的扩展类型(A Pigeon?或者可能是Eagle?),我唯一知道的是它将是一个Bird.
如果 Pigeon extends Bird
使用多态性我可以将一只鸟声明为:
Bird tom = new Pigeon();而不是Pigeon tom = new Pigeon();
那么为什么我不能在构造函数中初始化类似的东西:[...]
private List<Bird> birdList;
public Cage() {
this.birdList = new ArrayList<Pigeon>();
/* Instead of birdList = new ArrayList<Bird>(); */
}
Run Code Online (Sandbox Code Playgroud)
如果你不能这样做,是否有可能以另一种方式实现我的目标?
java ×10
generics ×8
android ×1
casting ×1
inheritance ×1
java-8 ×1
lambda ×1
list ×1
oop ×1
parcelable ×1
polymorphism ×1
retrofit ×1
retrofit2 ×1