当前位置:网站首页>The embodiment of generics in inheritance and wildcards
The embodiment of generics in inheritance and wildcards
2022-07-03 07:21:00 【four thousand three hundred and forty-three】

List<String> And List<Object>, There is no parent-child relationship
List<Object> And ArrayList<Object> There is a parent-child relationship
wildcard :
<?>

Requirements for reading and writing wildcards :
about list<?> Cannot add data in : list.add() no way , In addition to adding Null outside
Read :( Can only read to Object)

The use of restricted wildcards :


package TongPeiFu;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class test {
@Test
public void test(){
List<Object> list1 = null;
List<String> list2 = null;
List<?> list = null;
list = list1;
list = list2;
List<String > list3 = new ArrayList<>();
list3.add("aa");
list3.add("bb");
list3.add("cc");
list = list3;
}
public void show(List<?> list){
Iterator<?> iterator = list.iterator();
while (iterator.hasNext()){
Object obj = iterator.next();
System.out.println(obj);
}
}
@Test
public void test2(){
List<? extends Person> list1 = null;
List<? super Person> list2 = null;
List<Person> list3 = null;
List<Student> list4 = null;
List<Object> list5 = null;
// ? extends Person here ? finger Person Subclass of class or Person Class itself , When you can't Person The parent of a class (Object)
list1 = list3;
list1 = list4;
// list1 = list5;
// ? super Person Empathy ;
// Receive data
list3 = new ArrayList<Person>();
list4 = new ArrayList<Student>();
list5 = new ArrayList<Object>();
list1= list4;
// Student st = list1.get(0); list1 The generics of are ?extends Person Of , So at least you can only use Person receive
list2 = list3;
// list2 The generic structure is <? super Person> Only use Object Object acceptance of
Object obj = list2.get(0);
// Write data
// about list1 Speaking of : Its generic structure is Person Subclasses of , So I'm not sure
// list1.add(new Student()); Can not be
// list1.add(new Person()); Can not be
// about list2 Speaking of : Its generic structure is Person Parent class of , therefore Person Class and its subclasses can be written
list2.add(new Person());
list2.add(new Student());
// list2.add(new Object()); Can not be
}
}
class Person{}
class Student extends Person{}边栏推荐
- sharepoint 2007 versions
- Download address collection of various versions of devaexpress
- dataworks自定義函數開發環境搭建
- 高并发内存池
- Pits encountered in the use of El checkbox group
- Win 10 find the port and close the port
- When MySQL inserts Chinese into the database, there is a diamond question mark garbled code
- New stills of Lord of the rings: the ring of strength: the caster of the ring of strength appears
- [solved] sqlexception: invalid value for getint() - 'Tian Peng‘
- Chrome 98 Private Network Access problem w/ disabled web security: Request had no target IP address
猜你喜欢

Sorting, dichotomy

New stills of Lord of the rings: the ring of strength: the caster of the ring of strength appears

Mise en place d'un environnement de développement de fonctions personnalisées

Interview questions about producers and consumers (important)

Recursion, Fibonacci sequence

Win 10 find the port and close the port
![[vscode - vehicle plug-in reports an error] cannot find module 'xxx' or its corresponding type declarations Vetur(2307)](/img/7f/3d6b6ea5319f7165e07baf0a78d318.jpg)
[vscode - vehicle plug-in reports an error] cannot find module 'xxx' or its corresponding type declarations Vetur(2307)

High concurrency memory pool

C代码生产YUV420 planar格式文件

How to specify the execution order for multiple global exception handling classes
随机推荐
[most detailed] latest and complete redis interview book (50)
3311. Longest arithmetic
C code production YUV420 planar format file
【无标题】
C代码生产YUV420 planar格式文件
SecureCRT password to cancel session recording
LeetCode
Specified interval inversion in the linked list
Advanced API (byte stream & buffer stream)
php安装swoole扩展
Jeecg data button permission settings
[set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*
树莓派更新工具链
Some experiences of Arduino soft serial port communication
TypeScript let与var的区别
php安装composer
How to specify the execution order for multiple global exception handling classes
Pat grade a real problem 1166
POI excel percentage
gstreamer ffmpeg avdec解码数据流向分析