当前位置:网站首页>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{}
边栏推荐
- 【已解决】win10找不到本地组策略编辑器解决方法
- [attribute comparison] defer and async
- Hash table, generic
- Book recommendation~
- Jeecg data button permission settings
- 3311. 最长算术
- 【无标题】
- Mise en place d'un environnement de développement de fonctions personnalisées
- Use the jvisualvm tool ----- tocmat to start JMX monitoring
- centos php7.3安装redis扩展
猜你喜欢
[solved] sqlexception: invalid value for getint() - 'Tian Peng‘
New stills of Lord of the rings: the ring of strength: the caster of the ring of strength appears
POI excel percentage
Take you through the whole process and comprehensively understand the software accidents that belong to testing
TCP cumulative acknowledgement and window value update
Wireshark software usage
Basic components and intermediate components
《指環王:力量之戒》新劇照 力量之戒鑄造者亮相
docker建立mysql:5.7版本指定路径挂载不上。
PAT甲级真题1166
随机推荐
《指环王:力量之戒》新剧照 力量之戒铸造者亮相
PAT甲级真题1166
dataworks自定義函數開發環境搭建
Dora (discover offer request recognition) process of obtaining IP address
Advanced API (multithreading)
Advanced API (character stream & net for beginners)
Arduino 软串口通信 的几点体会
Summary of abnormal mechanism of interview
Crontab scheduled task
Common analysis with criteria method
MySQL transaction rollback, error points record
VMware virtual machine installation
萬卷書 - 價值投資者指南 [The Education of a Value Investor]
Longest common prefix and
Inno Setup 制作安装包
centos php7.3安装redis扩展
Setting up the development environment of dataworks custom function
JMeter test result output
Hash table, generic
How to specify the execution order for multiple global exception handling classes