当前位置:网站首页>Generics and reflection, this is enough
Generics and reflection, this is enough
2022-07-02 22:59:00 【Xiaopucai learns Java】
Catalog
Generic : A means of checking whether there are errors in the program at the compilation stage .
* Definition of a generic class :
* Benefits of using generics :
Limitations and rules of generics
1, Common methods of using generic parameters :
2, Generic methods that use generics for both return values and parameters
Two ,?super Number, Set the lower bound of the generic
3、 ... and ,? extents Number, Set the Generic upper limit
1, Get the class object ( Yes JVM produce , Globally unique )
2, Get the construction method through reflection .
adopt Method The way to get it , Calling method .
Generic : A means of checking whether there are errors in the program at the compilation stage .
* Definition of generics
The so-called generics are ambiguous types when defining classes , Specify the type when using
Generic :“<>” Use diamond operator ,< class >
* Definition of a generic class :
package Generic ;
public class Point<T> {
//T Type parameter
// here xy The type of is uncertain , But it's guaranteed xy Type consistency of ,xy The type of is determined when the object is generated
private T x;
private T y;
public T getX() {
return x;
}
public void setX(T x) {
this.x = x;
}
public T getY() {
return y;
}
public void setY(T y) {
this.y = y;
}
}
* Use generic classes :
Define generic parameters as String type , take T x convert to String x, In giving x An error will be reported when an integer value is assigned .
Use of multiple generics : Use different capital letters to specify different types .
* Benefits of using generics :
1, The compilation phase checks whether the types are consistent , Avoid the error problem of downcast casts .
2, Code is conducive to reuse .
Limitations and rules of generics
- Type parameters of generic types can only be reference types , Cannot use value type .
- A generic type can have more than one type parameter .
- Generic classes are not real classes , Out of commission instanceof Operator .
- Type parameters of generic classes cannot be used in static declarations .
- If generics are defined , No specific type is specified , The generic type is specified as Ojbect type .
- Generics use ? As a type wildcard , Indicates an unknown type , Can match any type . Because it's unknown , So you can't add elements .
- Type wildcard upper limit :<? extends T>,? Representative is T The type itself or T Subtypes of . Commonly used in generic methods , Avoid type conversion .
- Type wildcard lower limit .<? super T>,? representative T The type itself or T The father type of .
- In addition to wildcards, restrictions can be implemented , class 、 Generic parameters defined in interfaces and methods can also limit upper and lower limits .
** Generic methods :
1, Common methods of using generic parameters :
public T fun(T t){
return t;
}
2, Generic methods that use generics for both return values and parameters
public <T> T Point(T t){
return t;
}
If a class is a generic class , The type parameters of generic methods are independent of the generic parameters of classes . Only related to generic parameters defined by your own method .
One class is a general class , Still, generic methods can be defined . But defined generic methods , Can't be static Embellished , Because use static Modified class method , Cannot pass generic parameters to a generic .
Generic methods of generic classes can only be instantiated . Generic methods when defining generic , Use different capital letters , Avoid ambiguity .
Methods using generic parameters , Cannot be defined as static
Generic interface :
Generic interface , Use <> Diamond operator defined interface , When implementing an interface , You can keep generics , It can also be in the implementation class
wildcard :
All three wildcards can be set in classes and methods .
One ,?
Two ,?super Number, Set the lower bound of the generic
3、 ... and ,? extents Number, Set the Generic upper limit
The topmost type can only be Number and Number Subclasses of classes .
** Type Erasure
Generic : Grammatical sugar
Type Erasure : Generic information is actually only in the compilation stage , Get into JVM after , All information related to generics will be erased .
If no Generic upper limit is specified , Then all generic information will be erased as Object type ,
If a Generic upper limit is specified , Then erase to the corresponding generic upper limit type .
Generic classes and ordinary classes enter JVM after , It doesn't make any difference
after javac After compilation , Generate *.class file , Generics will disappear .
Reflection : The mechanism of dynamically obtaining information and calling objects is called reflection mechanism .
The foundation of all frameworks
stay JVM Runtime ,
about JVM Any of the classes in , Can dynamically obtain all properties and methods of this class .
about JVM Any object in , Can dynamically obtain the properties and methods of the object .
The type of reflection :
Runtime type :RTTI
Compile time type :
Person p = new Student();// Compile time type is Person, The runtime type is Student;
The runtime type can be obtained through reflection .
1, Get the class object ( Yes JVM produce , Globally unique )
a, call Class.forName( Package name . Class name )
b, Call class name .class obtain
c, Through object .getClass() obtain .
2, Get the construction method through reflection .
get Can only get public The attributes and methods of decoration .
getDeclared...() Get all properties and methods .
Break encapsulation :
Use class keyword , Including arrays , Generated after interface compilation class The file only corresponds to one Class object , This object has JVM produce , Using this object, you can get all the information of this class .
adopt Method The way to get it , Calling method .
边栏推荐
- Solve the error of changing the selected file when uploading excel file. Net:: err_ UPLOAD_ FILE_ CHANGED
- Go four singleton modes
- World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
- Baidu AI Cloud - create a face recognition application
- Array advanced improvement
- PMP项目整合管理
- wait解决僵尸进程
- 杰理之内置短按再长按,不管长按多长时间都是短按【篇】
- 数组进阶提高
- 钟薛高回应产品1小时不化:含固体成分 融化不能变成水
猜你喜欢
[LeetCode] 数组中的第K个最大元素【215】
【喜欢的诗词】好了歌
[chestnut sugar GIS] ArcMap - how to batch modify the font, color, size, etc. of annotation elements
uniapp微信登录返显用户名和头像
电商系统微服务架构
World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
Jatpack------LiveData
[chestnut sugar GIS] ArcMap - why should the tick of classic capture be removed when using custom capture?
kubernetes 使用主机名将 pod 分配在指定节点上
从2022年Q1财报看携程的韧性和远景
随机推荐
Higher order operation of bits
PMP项目整合管理
送给即将工作的自己
pytorch训练CPU占用持续增长(bug)
解决Chrome浏览器和Edeg浏览器主页被篡改的方法
地方经销商玩转社区团购模式,百万运营分享
Comprehensively analyze the logic of the shared purchase business model? How sharing purchase empowers Enterprises
go 4種單例模式
Array advanced improvement
Freshman learning sharing
【板栗糖GIS】arcmap—为什么使用自定义捕捉的时候,经典捕捉的勾要去掉呢?
[leetcode] there are duplicate elements [217]
Analyse des données dossiers d'apprentissage - - analyse simple de la variance à facteur unique avec Excel
odoo13搭建医院HRP环境(详细步骤)
[leetcode] reverse the word III in the string [557]
WebRTC音视频采集和播放示例及MediaStream媒体流解析
數據分析學習記錄--用EXCEL完成簡單的單因素方差分析
Niuke: Dragon and dungeon games
杰理之充电拔出,无法触摸开机【篇】
最小生成树 Minimum Spanning Tree