Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Description of PE and CS principles for Java generics

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "PE and CS principles of Java generics". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn the PE and CS principles of Java generics.

What is PE,CS?

PE,CS is an acronym for producer extends,consumer super, a slightly strange term introduced by Joshua Bloch in Effective Java, but it helps to understand the use of generics. In other words, parameterized types represent producers (producer) use extends and represent consumers (consumer) use super

PE principle

To put it simply, PE means that if your method just wants to get a value from the collection and you want the type range of the collection to be T and its subclasses, then the generics can be defined as? Extends T

Students who know generics should know that extends is an upper-bound wildcard, using the upper-bound wildcard, can only read the value, not write the value, the type of value is T

What can only be taken as a value but not written is the producer. Some people may understand it a little bit. Let's explain it in more detail. Take a chestnut.

Suppose we have a fruit object, and the fruit class has an addAll method that is used to put another fruit collection into the fruit object.

Class Fruit {private List fruits = new LinkedList (); public void addAll (List fruits) {for (Fruit fruit: fruits) {this.fruits.add (fruit);}

Now, our requirements have changed. Apples and bananas inherit the fruit class respectively. If we put the apple or banana set directly into the addAll method, we will compile the error, because the apple set is not a fruit set.

Public static void main (String [] args) {List appleList = new LinkedList (); Fruit fruit = new Fruit (); / / compile error fruit.addAll (appleList);} class Apple extends Fruit {} class Banana extends Fruit {}

What if we want to put it in? use the upper-bound wildcard and modify the addAll method. After using the upper-bound wildcard, the element can only be read, not written, and the range of collection types passed in is Fruit or its subclass collection.

Public void addAll (List

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report