当前位置:网站首页>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 .
边栏推荐
- JS syntax ES6, ES7, es8, es9, ES10, es11, ES12 new features (Abstract)
- wait解决僵尸进程
- Go语言sqlx库操作SQLite3数据库增删改查
- Qt QSplitter拆分器
- Qt QScrollArea
- Qt QProgressBar详解
- LeetCode 968. 监控二叉树
- 【喜欢的诗词】好了歌
- Share 10 JS closure interview questions (diagrams), come in and see how many you can answer correctly
- Niuke network: maximum submatrix
猜你喜欢
[error record] the flutter reports an error (could not read script 'xxx\flutter\u tools\gradle\app\u plugin\u loader.gradle')
Qt QScrollArea
QT qsplitter splitter
LeetCode 968. Monitor binary tree
Qt QScrollArea
海思3559万能平台搭建:在截获的YUV图像上画框
[chestnut sugar GIS] how does global mapper batch produce ground contour lines through DSM
AES高級加密協議的動機闡述
Mask R-CNN
Uniapp wechat login returns user name and Avatar
随机推荐
存储单位换算
大话云原生之负载均衡篇-小饭馆客流量变大了
Jericho's thimble reaction when directly touching the prototype is abnormal [chapter]
[NPUCTF2020]ezlogin xPATH注入
解决Chrome浏览器和Edeg浏览器主页被篡改的方法
移动端 1px 像素兼容性问题,实现1px 边框
How does Jerry test the wrong touch rate of keys [chapter]
AES高级加密协议的动机阐述
How can I use knockout's $parent/$root pseudovariables from inside a . computed() observable?
Golang的学习路线
Wait to solve the zombie process
[LeetCode] 数组中的第K个最大元素【215】
杰理之如何测试按键的误触率【篇】
悬镜安全在RSAC2022上斩获Global InfoSec Awards四项大奖
[LeetCode] 反转字符串【344】
Go multithreaded data search
[chestnut sugar GIS] ArcMap - why should the tick of classic capture be removed when using custom capture?
[leetcode] number of palindromes [9]
大一学习分享
Golang interview finishing three resumes how to write