什么是PECS?

  Java   13分钟   730浏览   0评论

先来看一个错误:

List<? extends Foo> list1 = new ArrayList<Foo>(); 
List<? extends Foo> list2 = new ArrayList<Foo>();

/* Won't compile */ 
list2.add( new Foo() ); //error 1 
list1.addAll(list2);     //error 2

error 1:

IntelliJ says:

add(capture<? extends Foo>) in List cannot be applied to add(Foo)

The compiler says:

cannot find symbol
symbol : method addAll(java.util.List<capture#692 of ? extends Foo>)
location: interface java.util.List<capture#128 of ? extends Foo>

error 2:

IntelliJ gives me

addAll(java.util.Collection<? extends capture<? extends Foo>>) in List cannot be applied to addAll(java.util.List<capture<? extends Foo>>)

Whereas the compiler just says

cannot find symbol
symbol : method addAll(java.util.List<capture#692 of ? extends Foo>)
location: interface java.util.List<capture#128 of ? extends Foo> list1.addAll(list2);

下面会一步一步,逐步推导出,为什么会出现上面的错误,以及背后的原因

PECS法则

在这里插入图片描述

Apple 是Fruit 的子类,但是 List< Apple> 不是List< Fruit>的子类,那么有没有办法能让他两兼容使用呢?答案是:有,那就是协变和逆变。

主要是 extends 和 super 关键字。比如:

协变

HashMap< T extends String>;
HashMap< ? extends String>;

逆变

HashMap< T super String>; 
HashMap< ? super String>;

这是Java泛型中重要的PECS法则

协变 < ? extends T>

类型的上界是 T,参数化类型可能是 T 或 T 的子类:

public class Test { 
    static class Food {} 
    static class Fruit extends Food {} 
    static class Apple extends Fruit {} 

    public static void main(String[] args) throws IOException {
    List<? extends Fruit> fruits = new ArrayList<>(); 
    //不能加入任何元素 
    fruits.add(new Food()); // compile error
    fruits.add(new Fruit()); // compile error
    fruits.add(new Apple()); // compile error 

    //集合元素的类型,符合extends Fruit,可赋值给 变量fruits 
    fruits = new ArrayList<Food>(); // compile error 
    fruits = new ArrayList<Fruit>(); // compile success
    fruits = new ArrayList<Apple>(); // compile success 注1 
    fruits.add(new Apple()); // compile error 注2

    fruits = new ArrayList<? extends Fruit>(); // 在java中会出现 compile error: 通配符类型无法实例化
    Fruit object = fruits.get(0); // compile success 
      } 
}
如上的注1和注2 ,两条语句在kotlin中,AS不报错,可以正常运行。
把kotlin转为java,发现java代码 根本没有协变,但是其它错误语句在AS中是报错的,
我把java代码,贴到AS中是没有报错的。
可能的结论就 是,AS检查 优先级更高 ,有报错 就无法运行。
无报错,就按照java代码去执行。
难道是AS在检查Kotlin 的时候不够严谨

存入数据:

编译器会阻止将Apple类加入fruits。在向fruits中添加元素时,编译器会检查类型是否符合要求。因为编译器只知道fruits是Fruit某个子类的List,但并不知道这个子类具体是什么类,只好阻止向其中加入任何子类。为了类型安全,不能往使用了? extends的数据结构里写入任何的值。

元素类型 为 Fruit 和其子类的集合都可以成功赋值给变量fruits,赋值后,变量fruits类型就是具体的类型(不再是协变)。

通配符类型无法实例化

读取数据:

但是,由于编译器知道它总是Fruit的子类型,因此我们总可以从中读取出Fruit对象:

Fruit fruit = fruits.get(0);

kotlin 的协变——out

从关键字,也能看出,只能读出数据

var fruits :MutableList<out Fruit>

逆变<? super T>

表示类型的下界是 T,参数化类型可以是 T 或 T 的超类:

public class Test { 
    static class Food {} 
    static class Fruit extends Food {} 
    static class Apple extends Fruit {} 

    public static void main(String[] args) throws IOException {
    List<? super Fruit> fruits = new ArrayList<>(); 
    //Fruit 及其子类,可被看做是Fruit,从而添加成功
    fruits.add(new Food()); // compile error
    fruits.add(new Fruit()); // compile success
    fruits.add(new Apple()); // compile success

    //集合元素的类型,符合super Fruit,可赋值给变量fruits,赋值后fruits不再是逆变类型 
    fruits = new ArrayList<Food>(); // compile success
    fruits = new ArrayList<Fruit>(); // compile success
    fruits = new ArrayList<Apple>(); //  compile error

    fruits = new ArrayList<? super Fruit>(); // compile error: 通配符类型无法实 例化
    Fruit object = fruits.get(0); // compile error
      } 
}

kotlin 的逆变——in

从关键字,也能看出,只能写入数据

var fruits :MutableList<out Fruit>

存入数据:

① 添加 Fruit 及其子类 元素均可成功,因为编译器会自动向上转型,Fruit 及其子类 元素 可被认为是Fruit类型,可以成功添加 ,但由于编译器并不知道List的内容究竟是Fruit的哪个超类,因此不允许加入特定的任何超类型。

② 元素类型 为 Fruit 和其超类的 集合都可以成功赋值给变量fruits,赋值后,变量fruits类型就是具体的类型(不再是逆变)。

③ super 通配符类型同样不能实例化

读取数据

编译器在不知道这个超类具体是什么类,只能返回Object对象,因为Object是任何Java类的最终祖先类。

Object fruit = apples.get(0);

数组是协变的

在 Java 语言中,数组是协变的,如果 Number是 Integer 的超类型,那么 Number[] 也是 Integer[]的超类型)。

对于数组来说,String[] 是可以赋值给Object[]:

public class Test {
    public static void main(String[] args) {
        String[] strArray = new String[3];
        Object[] objArray = strArray;
    } 
}

kotlin的泛型实化

泛型实化在Java中是个不存在的概念,属于Kotlin的新特性;它能在运行时保留泛型信息,这听起来违反了JVM的机制,但是它确实可以做到,而且原理很简单,主要借助于关键字 inline 。

不使用inline

fun <T> create(): T = mRetrofit.create(T::class.java)

因为T在编译时不存在,所以没法通过T拿到T的class对象。

使用inline

inline fun <reified T> create(): T = mRetrofit.create(T::class.java)

这个方法不仅可以被合法声明,而且在调用时也会非常优雅。

val service = create<NetworkService>()

create()方法不接收任何对象作为参数,而是只是传入了一个类型参数,就可以根据传入类型的不同返回我们需要的对象。

原理很简单,任何被声明称inline的函数都会把函数体内的所有代码直接复制到每一个被调用的地方,而由于泛型参数值的不同,所以每一个调用inline函数的位置都会因为泛型参数值的不同而有所不同,所以它在编译器就能确定具体的类型,才能实例化。

PECS原则总结

从上述两方面的分析,总结PECS原则如下:

  1. 如果要从集合中读取类型T的数据,并且不能写入,可以使用 ? extends 通配符;(Producer Extends)

  2. 如果要从集合中写入类型T的数据,并且不需要读取,可以使用 ? super 通配符;(Consumer Super)

  3. 如果既要存又要取,那么就不要使用任何通配符

如果你觉得文章对你有帮助,那就请作者喝杯咖啡吧☕
微信
支付宝
😀
😃
😄
😁
😆
😅
🤣
😂
🙂
🙃
😉
😊
😇
🥰
😍
🤩
😘
😗
☺️
😚
😙
🥲
😋
😛
😜
🤪
😝
🤑
🤗
🤭
🫢
🫣
🤫
🤔
🤨
😐
😑
😶
😏
😒
🙄
😬
😮‍💨
🤤
😪
😴
😷
🤒
🤕
🤢
🤮
🤧
🥵
🥶
🥴
😵
😵‍💫
🤯
🥳
🥺
😠
😡
🤬
🤯
😈
👿
💀
☠️
💩
👻
👽
👾
🤖
😺
😸
😹
😻
😼
😽
🙀
😿
😾
👋
🤚
🖐️
✋️
🖖
🫱
🫲
🫳
🫴
🫷
🫸
👌
🤌
🤏
✌️
🤞
🫰
🤟
🤘
🤙
👈️
👉️
👆️
🖕
👇️
☝️
🫵
👍️
👎️
✊️
👊
🤛
🤜
👏
🙌
👐
🤲
🤝
🙏
✍️
💅
🤳
💪
🦾
🦿
🦵
🦶
👂
🦻
👃
👶
👧
🧒
👦
👩
🧑
👨
👩‍🦱
👨‍🦱
👩‍🦰
👨‍🦰
👱‍♀️
👱‍♂️
👩‍🦳
👨‍🦳
👩‍🦲
👨‍🦲
🧔‍♀️
🧔‍♂️
👵
🧓
👴
👲
👳‍♀️
👳‍♂️
🧕
👮‍♀️
👮‍♂️
👷‍♀️
👷‍♂️
💂‍♀️
💂‍♂️
🕵️‍♀️
🕵️‍♂️
👩‍⚕️
👨‍⚕️
👩‍🌾
👨‍🌾
👩‍🍳
👨‍🍳
🐶
🐱
🐭
🐹
🐰
🦊
🐻
🐼
🐨
🐯
🦁
🐮
🐷
🐸
🐵
🐔
🐧
🐦
🦅
🦉
🐴
🦄
🐝
🪲
🐞
🦋
🐢
🐍
🦖
🦕
🐬
🦭
🐳
🐋
🦈
🐙
🦑
🦀
🦞
🦐
🐚
🐌
🦋
🐛
🦟
🪰
🪱
🦗
🕷️
🕸️
🦂
🐢
🐍
🦎
🦖
🦕
🐊
🐢
🐉
🦕
🦖
🐘
🦏
🦛
🐪
🐫
🦒
🦘
🦬
🐃
🐂
🐄
🐎
🐖
🐏
🐑
🐐
🦌
🐕
🐩
🦮
🐕‍🦺
🐈
🐈‍⬛
🐓
🦃
🦚
🦜
🦢
🦩
🕊️
🐇
🦝
🦨
🦡
🦫
🦦
🦥
🐁
🐀
🐿️
🦔
🌵
🎄
🌲
🌳
🌴
🌱
🌿
☘️
🍀
🎍
🎋
🍃
🍂
🍁
🍄
🌾
💐
🌷
🌹
🥀
🌺
🌸
🌼
🌻
🌞
🌝
🌛
🌜
🌚
🌕
🌖
🌗
🌘
🌑
🌒
🌓
🌔
🌙
🌎
🌍
🌏
🪐
💫
🌟
🔥
💥
☄️
☀️
🌤️
🌥️
🌦️
🌧️
⛈️
🌩️
🌨️
❄️
☃️
🌬️
💨
💧
💦
🌊
🍇
🍈
🍉
🍊
🍋
🍌
🍍
🥭
🍎
🍏
🍐
🍑
🍒
🍓
🥝
🍅
🥥
🥑
🍆
🥔
🥕
🌽
🌶️
🥒
🥬
🥦
🧄
🧅
🍄
🥜
🍞
🥐
🥖
🥨
🥯
🥞
🧇
🧀
🍖
🍗
🥩
🥓
🍔
🍟
🍕
🌭
🥪
🌮
🌯
🥙
🧆
🥚
🍳
🥘
🍲
🥣
🥗
🍿
🧈
🧂
🥫
🍱
🍘
🍙
🍚
🍛
🍜
🍝
🍠
🍢
🍣
🍤
🍥
🥮
🍡
🥟
🥠
🥡
🦪
🍦
🍧
🍨
🍩
🍪
🎂
🍰
🧁
🥧
🍫
🍬
🍭
🍮
🍯
🍼
🥛
🍵
🍶
🍾
🍷
🍸
🍹
🍺
🍻
🥂
🥃
🥤
🧃
🧉
🧊
🗺️
🏔️
⛰️
🌋
🏕️
🏖️
🏜️
🏝️
🏞️
🏟️
🏛️
🏗️
🏘️
🏙️
🏚️
🏠
🏡
🏢
🏣
🏤
🏥
🏦
🏨
🏩
🏪
🏫
🏬
🏭
🏯
🏰
💒
🗼
🗽
🕌
🛕
🕍
⛩️
🕋
🌁
🌃
🏙️
🌄
🌅
🌆
🌇
🌉
🎠
🎡
🎢
💈
🎪
🚂
🚃
🚄
🚅
🚆
🚇
🚈
🚉
🚊
🚝
🚞
🚋
🚌
🚍
🚎
🚐
🚑
🚒
🚓
🚔
🚕
🚖
🚗
🚘
🚙
🚚
🚛
🚜
🏎️
🏍️
🛵
🦽
🦼
🛺
🚲
🛴
🛹
🚏
🛣️
🛤️
🛢️
🚨
🚥
🚦
🚧
🛶
🚤
🛳️
⛴️
🛥️
🚢
✈️
🛩️
🛫
🛬
🪂
💺
🚁
🚟
🚠
🚡
🛰️
🚀
🛸
🧳
📱
💻
⌨️
🖥️
🖨️
🖱️
🖲️
💽
💾
📀
📼
🔍
🔎
💡
🔦
🏮
📔
📕
📖
📗
📘
📙
📚
📓
📒
📃
📜
📄
📰
🗞️
📑
🔖
🏷️
💰
💴
💵
💶
💷
💸
💳
🧾
✉️
📧
📨
📩
📤
📥
📦
📫
📪
📬
📭
📮
🗳️
✏️
✒️
🖋️
🖊️
🖌️
🖍️
📝
📁
📂
🗂️
📅
📆
🗒️
🗓️
📇
📈
📉
📊
📋
📌
📍
📎
🖇️
📏
📐
✂️
🗃️
🗄️
🗑️
🔒
🔓
🔏
🔐
🔑
🗝️
🔨
🪓
⛏️
⚒️
🛠️
🗡️
⚔️
🔫
🏹
🛡️
🔧
🔩
⚙️
🗜️
⚗️
🧪
🧫
🧬
🔬
🔭
📡
💉
🩸
💊
🩹
🩺
🚪
🛏️
🛋️
🪑
🚽
🚿
🛁
🧴
🧷
🧹
🧺
🧻
🧼
🧽
🧯
🛒
🚬
⚰️
⚱️
🗿
🏧
🚮
🚰
🚹
🚺
🚻
🚼
🚾
🛂
🛃
🛄
🛅
⚠️
🚸
🚫
🚳
🚭
🚯
🚱
🚷
📵
🔞
☢️
☣️
❤️
🧡
💛
💚
💙
💜
🖤
💔
❣️
💕
💞
💓
💗
💖
💘
💝
💟
☮️
✝️
☪️
🕉️
☸️
✡️
🔯
🕎
☯️
☦️
🛐
🆔
⚛️
🉑
☢️
☣️
📴
📳
🈶
🈚
🈸
🈺
🈷️
✴️
🆚
💮
🉐
㊙️
㊗️
🈴
🈵
🈹
🈲
🅰️
🅱️
🆎
🆑
🅾️
🆘
🛑
💢
💯
💠
♨️
🚷
🚯
🚳
🚱
🔞
📵
🚭
‼️
⁉️
🔅
🔆
🔱
⚜️
〽️
⚠️
🚸
🔰
♻️
🈯
💹
❇️
✳️
🌐
💠
Ⓜ️
🌀
💤
🏧
🚾
🅿️
🈳
🈂️
🛂
🛃
🛄
🛅
  0 条评论