当前位置:网站首页>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 .



边栏推荐
猜你喜欢

Webrtc audio and video capture and playback examples and mediastream media stream analysis

Kubernetes uses the host name to allocate the pod on the specified node

Performance optimization - rigorous mode

最小生成树 Minimum Spanning Tree

Jatpack------LiveData
![[NPUCTF2020]ezlogin xPATH注入](/img/6e/dac4dfa0970829775084bada740542.png)
[NPUCTF2020]ezlogin xPATH注入

`Usage of ${}`

小鹏P7出事故,安全气囊未弹出,这正常吗?

Addition, deletion, modification and query of handwritten ORM (object relationship mapping)
![[LeetCode] 多数元素【169】](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[LeetCode] 多数元素【169】
随机推荐
数组进阶提高
go 4種單例模式
Go four singleton modes
How does Jerry test the wrong touch rate of keys [chapter]
Wait to solve the zombie process
[chestnut sugar GIS] ArcScene - how to make elevation map with height
LC173. 二叉搜索树迭代器
手写ORM(对象关系映射)增删改查
百度智能云-创建人脸识别应用
xshell配置xforward转发火狐浏览器
泛型与反射,看这篇就够了
Data analysis learning records -- complete a simple one-way ANOVA with Excel
Jerry's built-in short press and long press, no matter how long it is, it is a short press [chapter]
[chestnut sugar GIS] how does global mapper batch produce ground contour lines through DSM
[error record] the flutter reports an error (could not read script 'xxx\flutter\u tools\gradle\app\u plugin\u loader.gradle')
大一学习分享
JS syntax ES6, ES7, es8, es9, ES10, es11, ES12 new features (Abstract)
[leetcode] most elements [169]
kubernetes 使用主机名将 pod 分配在指定节点上
Jerry's charge unplugged, unable to touch the boot [chapter]