当前位置:网站首页>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{}
边栏推荐
- Jmeter+influxdb+grafana of performance tools to create visual real-time monitoring of pressure measurement -- problem record
- Spa single page application
- JS date comparison
- [cmake] cmake link SQLite Library
- "Moss ma not found" solution
- Gridome + strapi + vercel + PM2 deployment case of [static site (3)]
- 7.2刷题两个
- TypeScript let与var的区别
- Laravel Web框架
- 【已解决】SQLException: Invalid value for getInt() - ‘田鹏‘
猜你喜欢
[Fiddler problem] solve the problem about Fiddler's packet capturing. After the mobile network is configured with an agent, it cannot access the Internet
Gridome + strapi + vercel + PM2 deployment case of [static site (3)]
Mise en place d'un environnement de développement de fonctions personnalisées
Distributed transactions
深度学习参数初始化(一)Xavier初始化 含代码
Wireshark software usage
3311. 最长算术
Jmeter+influxdb+grafana of performance tools to create visual real-time monitoring of pressure measurement -- problem record
Final, override, polymorphism, abstraction, interface
[solved] unknown error 1146
随机推荐
Advanced API (serialization & deserialization)
在 4EVERLAND 上存储 WordPress 媒体内容,完成去中心化存储
Laravel Web Framework
Arduino 软串口通信 的几点体会
[Fiddler actual operation] how to use Fiddler to capture packets on Apple Mobile Phones
Distributed lock
JMeter JSON extractor extracts two parameters at the same time
Sorting, dichotomy
Interview questions about producers and consumers (important)
MySQL transaction rollback, error points record
Some experiences of Arduino soft serial port communication
C代码生产YUV420 planar格式文件
When MySQL inserts Chinese into the database, there is a diamond question mark garbled code
Inno setup production and installation package
[set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*
sharepoint 2007 versions
LeetCode
Basic knowledge about SQL database
Strategy mode
Split small interface