当前位置:网站首页>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{}
边栏推荐
- 《指環王:力量之戒》新劇照 力量之戒鑄造者亮相
- 萬卷書 - 價值投資者指南 [The Education of a Value Investor]
- mysql误删root账户导致无法登录
- 【已解决】win10找不到本地组策略编辑器解决方法
- Advanced API (batch image Download & socket dialog)
- Chrome 98 Private Network Access problem w/ disabled web security: Request had no target IP address
- Distributed ID
- 3311. 最长算术
- GStreamer ffmpeg avdec decoded data flow analysis
- [set theory] partition (partition | partition example | partition and equivalence relationship)
猜你喜欢
带你全流程,全方位的了解属于测试的软件事故
[set theory] partition (partition | partition example | partition and equivalence relationship)
Inno Setup 制作安装包
File operation serialization recursive copy
“百度杯”CTF比赛 2017 二月场,Web:爆破-1
691. 立方体IV
Mise en place d'un environnement de développement de fonctions personnalisées
Gridome + strapi + vercel + PM2 deployment case of [static site (3)]
691. Cube IV
高并发内存池
随机推荐
[untitled]
File operation serialization recursive copy
Pat grade a real problem 1166
Mise en place d'un environnement de développement de fonctions personnalisées
CentOS switches and installs mysql5.7 and mysql8.0
JMeter JSON extractor extracts two parameters at the same time
Final, override, polymorphism, abstraction, interface
[Fiddler problem] solve the problem about Fiddler's packet capturing. After the mobile network is configured with an agent, it cannot access the Internet
4279. 笛卡尔树
[solved] unknown error 1146
Download address collection of various versions of devaexpress
Sorting, dichotomy
Advanced API (use of file class)
Basic knowledge about SQL database
php安装composer
MySQL mistakenly deleted the root account and failed to log in
Use the jvisualvm tool ----- tocmat to start JMX monitoring
twenty million two hundred and twenty thousand three hundred and nineteen
[most detailed] latest and complete redis interview book (50)
Inno setup production and installation package