当前位置:网站首页>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{}边栏推荐
- I. D3.js hello world
- II. D3.js draw a simple figure -- circle
- New stills of Lord of the rings: the ring of strength: the caster of the ring of strength appears
- php安装swoole扩展
- 2. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log
- PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef
- dataworks自定义函数开发环境搭建
- Pits encountered in the use of El checkbox group
- Mise en place d'un environnement de développement de fonctions personnalisées
- [attribute comparison] defer and async
猜你喜欢

Inno Setup 制作安装包

Pits encountered in the use of El checkbox group

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

dataworks自定义函数开发环境搭建

专题 | 同步 异步
![[Fiddler actual operation] how to use Fiddler to capture packets on Apple Mobile Phones](/img/d0/850e095a43610366d6144b2471f3f7.jpg)
[Fiddler actual operation] how to use Fiddler to capture packets on Apple Mobile Phones

Take you through the whole process and comprehensively understand the software accidents that belong to testing

Le Seigneur des anneaux: l'anneau du pouvoir

Recursion, Fibonacci sequence

带你全流程,全方位的了解属于测试的软件事故
随机推荐
The difference between typescript let and VaR
sharepoint 2007 versions
20220319
Homology policy / cross domain and cross domain solutions /web security attacks CSRF and XSS
crontab定时任务
【已解决】SQLException: Invalid value for getInt() - ‘田鹏‘
1. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log
centos php7.2.24升级到php7.3
POI excel percentage
II. D3.js draw a simple figure -- circle
Inno setup production and installation package
docket
docker建立mysql:5.7版本指定路径挂载不上。
Gridome + strapi + vercel + PM2 deployment case of [static site (3)]
Download address collection of various versions of devaexpress
Laravel frame step pit (I)
Centos切换安装mysql5.7和mysql8.0
Common APIs
Advanced API (multithreading 02)
691. 立方体IV