What is PECS, elaborate in Java extends and super

Relationship classes as follows:

	private static class Food {
    }

    private static class Fruit extends Food {
    }

    private static class Apple extends Fruit {
    }

    private static class Lemon extends Fruit {
    }

Wrapper class

private static class Box<T> {
        private final String boxName;

        private T thing;

        public Box(String boxName) {
            this.boxName = boxName;
        }

        public T getThing() {
            return thing;
        }

        public void setThing(T thing) {
            this.thing = thing;
        }
    }

Consumer PECS i.e. the Extends Super Producer
Super, the lower bound wildcard (Lower Bounds Wildcards), Fruit and its base classes belonging to the lower bound
extends, upper bounded wildcards (Upper Bounds Wildcards), Fruit and its subclasses belonging bound

  • A few questions :
  1. Box <? Super Fruit> What types can represent
		new Box<Fruit>
        new Box<Food>
  1. Box <? Extends Fruit> What types can represent
        new new Box<Fruit>
        new new Box<Apple>
        new new Box<Lemon>
  1. PECS is the time from the perspective of the collection, the collection is part of the producer (provided data), or a consumer (usage data)

    3.1 Why Producers Producer (Box <? Extends Fruit> ), which can only get data from the collection, but not add data?
    <? The extends Fruit> Box Box = new new Box <> ( "box1");
    // Box may point to new new Box or new new Box or new new Box,
    // we can not guarantee what is kept inside a specific type, only out to ensure that the type of Fruit

    3.2 Why Consumers Consumer (Box <? Super Fruit> ), can only add data, you can not get data from the collection inside?
    <? Super Fruit> Box Box = new Box <> ( "box1");
    box.add (new new Fruit ());
    // Box may point to new Box or the new Box, but it certainly can be placed Fruit type, you can add data
    // but we can not guarantee get out of the data is Fruit type (we get is to remove certain types of data), or may be Fruit Food,
    // so you can not get a generic Fruit type of data rather than the data can not get

    3.3 Consumer (Box <? Super Fruit>) What type of data can add?
    <? Super Fruit> Box Box = new Box <> ( "box1");
    // Box may point to new Box or the new Box, but it certainly can be placed Fruit type
    box.add (new new the Apple ());
    box.add ( Lemon new new ());
    box.add (new new Fruit ());
    box.add (new new Food ()); // error

  • Test code :
		// 可以指向Fruit及其基类
		Box<? super Fruit> box1 = new Box<Fruit>("box1");
        Box<? super Fruit> box2 = new Box<Food>("box1");
		// 可以指向Fruit及其子类
        Box<? extends Fruit> box11 = new Box<Fruit>("box1");
        Box<? extends Fruit> box12 = new Box<Apple>("box1");
        
        // 消费者,add数据的类型
		Box<? super Fruit> box = new Box<>("box1");
        box.setThing(new Apple());
        box.setThing(new Lemon());
        box.setThing(new Fruit());
        box.setThing(new Food()); // error
        // 消费者,无法get(Fruit类型的数据)
        Fruit fruit = box.getThing(); // error
        
        // 生产者,不能add
		Box<? extends Fruit> box2 = new Box<>("box2");
		box2.setThing(new Apple()); // error
  • refer
  • https://www.cnblogs.com/drizzlewithwind/p/6100164.html
  • https://stackoverflow.com/questions/2723397/what-is-pecs-producer-extends-consumer-super
  • https://stackoverflow.com/questions/2776975/how-can-i-add-to-list-extends-number-data-structures
Released five original articles · won praise 1 · views 1215

Guess you like

Origin blog.csdn.net/linshenggui/article/details/104657897