PECS( Producer Extends, Consumer Super)
- Consumer:
向上转型不适用:运行时 grandFather是一个Father 数组
public class CovariantArrays {
public static void main(String... args) {
GrandFather[] grandFather = new Father[10];
grandFather[0] = new Son();
grandFather[1] = new Father();
try {
// Runtime Error
grandFather[2] = new GrandFather();
} catch (Exception e) {
System.out.println(e); //java.lang.ArrayStoreException: GrandFather
}
}
}
class GrandFather {
int gf;
}
class Father extends GrandFather{
int f;
}
class Son extends Father{
int s;
}
使用<? extends T>
import java.util.List;
import java.util.ArrayList;
public class ExtendsTest {
public static void main(String... args) {
//error: incompatible types: ArrayList<Father> cannot be converted to List<GrandFather>
//List<GrandFather> grandFather = new ArrayList<Father>();
List<? extends Father> father = new ArrayList<Father>();
try {
// compile error: no suitable method found for add(Son)
father.add(new Son());
// compile error: no suitable method found for add(Father)
father.add(new Father());
// compile error: no suitable method found for add(GrandFather)
father.add(new GrandFather());
} catch (Exception e) {
System.out.println(e); //java.lang.ArrayStoreException: GrandFather
}
}
}
class GrandFather {
int gf;
}
class Father extends GrandFather{
int f;
}
class Son extends Father{
int s;
}
<? super T>
import java.util.List;
import java.util.ArrayList;
public class SuperTest {
public static void main(String... args) {
//error: incompatible types: ArrayList<Father> cannot be converted to List<GrandFather>
//List<GrandFather> grandFather = new ArrayList<Father>();
List<? super Father> father = new ArrayList<GrandFather>();
try {
father.add(new Son());
father.add(new Father());
// compile error: no suitable method found for add(GrandFather)
father.add(new GrandFather());
} catch (Exception e) {
System.out.println(e); //java.lang.ArrayStoreException: GrandFather
}
}
}
class GrandFather {
int gf;
}
class Father extends GrandFather{
int f;
}
class Son extends Father{
int s;
}
- Producer
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
public class ExtendsTest {
public static void main(String... args) {
List<? extends Father> father = Arrays.asList(new Son());
try {
Son s = (Son)father.get(0);
Father f = (Father)father.get(0);
GrandFather gf = (GrandFather)father.get(0);
father.contains(new Son());
father.indexOf(new Son());
} catch (Exception e) {
System.out.println(e);
}
}
}
class GrandFather {
int gf;
}
class Father extends GrandFather{
int f;
}
class Son extends Father{
int s;
}
import java.util.List;
import java.util.ArrayList;
public class SuperTest {
public static void main(String... args) {
List<? super Father> father = new ArrayList<GrandFather>();
try {
Son son = new Son();
System.out.print(son.toString()); //Son@4aa298b7
father.add(son);
Father f = new Father();
father.add(f);
System.out.println(father.contains(son)); //true
System.out.println(father.indexOf(f)); //1
Son s = (Son)father.get(0);
System.out.print(s.toString()); //Son@4aa298b7
} catch (Exception e) {
System.out.println(e);
}
}
}
class GrandFather {
int gf;
}
class Father extends GrandFather{
int f;
}
class Son extends Father{
int s;
}