当前位置:网站首页>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{}边栏推荐
- Sorting, dichotomy
- SharePoint modification usage analysis report is more than 30 days
- Common analysis with criteria method
- [plus de détails] dernière entrevue complète redis (50)
- Arduino Serial系列函数 有关print read 的总结
- I. D3.js hello world
- Take you through the whole process and comprehensively understand the software accidents that belong to testing
- Use the jvisualvm tool ----- tocmat to start JMX monitoring
- Raspberry pie update tool chain
- Le Seigneur des anneaux: l'anneau du pouvoir
猜你喜欢

高并发内存池

Common APIs

《指环王:力量之戒》新剧照 力量之戒铸造者亮相

7.2刷题两个

VMWare网络模式-桥接,Host-Only,NAT网络

1. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log

JMeter JSON extractor extracts two parameters at the same time

Basic components and intermediate components

JUC forkjoinpool branch merge framework - work theft

IP home online query platform
随机推荐
The education of a value investor
Download address collection of various versions of devaexpress
2. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log
Wireshark software usage
Le Seigneur des anneaux: l'anneau du pouvoir
Pits encountered in the use of El checkbox group
Jeecg menu path display problem
Store WordPress media content on 4everland to complete decentralized storage
Summary of abnormal mechanism of interview
POI excel percentage
PAT甲级真题1166
[solved] sqlexception: invalid value for getint() - 'Tian Peng‘
【已解决】SQLException: Invalid value for getInt() - ‘田鹏‘
Win 2008 R2 crashed at the final installation stage
[most detailed] latest and complete redis interview book (50)
High concurrency memory pool
Operation and maintenance technical support personnel have hardware maintenance experience in Hong Kong
Win 10 find the port and close the port
[plus de détails] dernière entrevue complète redis (50)
MySQL mistakenly deleted the root account and failed to log in