当前位置:网站首页>Understanding generics mechanism
Understanding generics mechanism
2022-06-21 06:49:00 【naoguaziteng】
Objective record 
5. Generics can be used in class , Interface , On the way
1. Generic mechanism 

The generic mechanism is a way to explicitly work with data types , Defer until you create an object or call a method , Just to clear a mechanism , That is, the data type of data is known only when an object is created or a method is called . And the generic mechanism is JDK1.5 Mechanism only after version !
2. The syntax of generics 

<E,Y> among E Y It's the code , Generally refers to the reference data type .
3. Note on generics 

Generics are only valid at compile time , At run time, generics are erased . That is, the function of generics is only valid at compile time .
4. Generic benefits : The problem can be compiled ahead of time , Generics have good extensibility and can avoid downward transformation . 

Generally speaking , Multiple data types are not stored in the container , Only the same data type is stored in the container . The intuitive feeling of using generics is that you can limit the data types of the elements stored in the collection , Only generic types are allowed .
ArrayList<String> list2 = new ArrayList<String>();
list2.add("abc");
list2.add("abc");
list2.add("hello");
list2.add("world");
// At this time, only String Type of data put into list2 Collection If there is a requirement that you want to store multiple reference data types in the collection and write generics ! You have to define generics as <Object>
ArrayList<Object> list3 = new ArrayList<>();
list3.add(200);
list3.add("abc");5. Generics can be used in class , Interface , On the way 

Generic classes : public class Class name < The generic type 1,…>
public class MyObject<X, Y> {
private X x;
private Y name;
private Date birthday;
public X getX() {
return x;
}
public void setX(X x) {
this.x = x;
}
public Y getName() {
return name;
}
public void setName(Y name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}Generic interface : public interface The interface name < The generic type >
public interface MyInterface<X> {
public abstract void show(X x);
}
When a class , To implement an interface , You can specify the specific data type of the generic defined on the generic interface here
public class AA implements MyInterface<Integer> {
@Override
public void show(Integer integer) {
}
}When a class implements a generic interface , I still don't want to be in a hurry to clarify the specific types of generics on this interface , So this class continues to be designed as a generic class
public class BB<X> implements MyInterface<X> {
@Override
public void show(X x) {
}
}Generic methods : public < The generic type > Return type Method name ( The generic type Variable name )
public class MyCalc {
public <P> void show(P p) {
System.out.println(p);
}
}
6. Generic wildcard <?> 

Generic wildcards refer to any type , If it's not clear , So that is Object And whatever Java The class . It is more arbitrary than Object To any degree !

As shown in the figure above :Object A generic can only be followed by <Object type >, Other String Or custom Animal No type . But wildcards are OK , It can also include Object Generic .
There are two kinds of generic types that can be followed by wildcards , If you are not satisfied with the situation, you will report an error !
- ? extends E: Down limit , You can only write E Class and its subclasses
- ? super E: Limit upward , You can only write E Class and its parent class
import java.util.ArrayList;
public class FanXingTest {
public static void main(String[] args) {
// Limit upward It can only be said Animal Itself or Animal Parent class of
ArrayList<? super Animal> objects4 = new ArrayList<Animal>();
ArrayList<? super Animal> objects5 = new ArrayList<Object>();
// Down limit It can only be said Animal Itself or Animal Subclasses of
ArrayList<? extends Animal> objects7 = new ArrayList<Dog>();
ArrayList<? extends Animal> objects8 = new ArrayList<Cat>();
ArrayList<? extends Animal> objects9 = new ArrayList<Animal>();
}
}
class Animal {
}
class Cat extends Animal {
}
class Dog extends Animal {
}
( Xiaobian is also trying to learn more ! I'll share it later !)

Hope to be helpful to friends !!!!
边栏推荐
- How to limit intranet speed
- Markdown mathematical grammar [detailed summary]
- Tweenmax irregular geometry background with animation JS effect
- The framework and script of cognitive linguistics
- Candy tunnel JS special effect code
- 156-Rust和Solana环境配置
- Blasting with burp (ordinary blasting + verification code blasting)
- 【MySQL】数据库多表操作通关教程(外键约束、多表联合查询)
- 动态规划习题(二)
- 递归建立链式二叉树,完成前中后序遍历以及其他功能(附源码)
猜你喜欢

0-1 knapsack problem (violent recursion / dynamic programming)

Unity hidden directories and hidden files

我的高考经历与总结

第4篇:从硬件/源码视角看JVM内存模型

【MySQL】数据库函数通关教程上篇(聚合、数学、字符串、日期、控制流函数)

Course design of simulated bank deposit and withdrawal management system in C language (pure C language version)

Summary of each layer of the five layer reference model

How to access MySQL database through JDBC? Hand to hand login interface (illustration + complete code)

Regular expression Basics

College entrance examination
随机推荐
【input】输入框事件总结
156 rust and Solana environment configuration
How to select PostgreSQL and MySQL
Data visualization practice: data processing
TweenMax不规则几何图形背景带动画js特效
[JS] intercepting string
5254. dynamic planning of selling wood blocks
Ztmao主题猫wordpress主题经典失传版/WP网站模板下载站源码+全局SEO功能设定
创新项目实训:数据爬取
麦克风loading动画效果
【查询数据表中第三行数据】
Old users come back and have a look
工作那点事
Part 4: JVM memory model from the perspective of hardware / source code
leetcode数据库mysql题目(难度:简单)
粽子大战 —— 猜猜谁能赢
GEO2R:对GEO数据库中的数据进行差异分析
六月集训(第21天) —— 堆(优先队列)
高考那些事
第4篇:从硬件/源码视角看JVM内存模型