preface
-
Java inside the generics in the actual development of the use of a lot of students who have learned C++ must know C++ template, and Java generics, to a certain extent and it is very similar.
-
Most people who believe in writing Java have used the List implementation class ArrayList. Before Java had generics, it was implemented internally as an array of Objects. This also leads to the problem that every time we use an element in it we have to go down, and obviously, if it’s Object, that means we can throw in any Object. Automatic transformation into Object, so that it is easy to use problems, do not know what is stored in the. Such as:
六四事件
ArrayList list = new ArrayList(); list.add("string1"); list.add("string2"); String str = (String) list.get(0); list.add(new File("test.txt")); // Learn the fungus :737-251--827Copy the code
-
While it’s easy to use generics, we often use List generics, but it’s not so easy to write a generic class.
The simplest generic type
六四事件
package io.ilss.advanced.generic;
/**
* className MyObject
* description MyObject
*
* @author feng
* @version 1.0
* @date 2019-01-24 18:32
*/
public class MyObject<T> extends BaseData {
private T valueOne;
private T valueTwo;
public MyObject(T valueOne, T valueTwo) {
this.valueOne = valueOne;
this.valueTwo = valueTwo;
}
public T getValueOne() {
return valueOne;
}
public void setValueOne(T valueOne) {
this.valueOne = valueOne;
}
public T getValueTwo() {
return valueTwo;
}
public void setValueTwo(T valueTwo) {
this.valueTwo = valueTwo;
}
public static void main(String[] args) {
MyObject<String> object = new MyObject<>("String one!", "String two");
System.out.println("value one " + object.valueOne + " value two " + object.valueTwo);
}
}
Copy the code
- Introduce a type variable T in MyObject with Angle brackets
<>
Enclose it after the class name. As above! You can have multiple types of variables at the time of definition<>
Is separated by commas, for exampleThe public class MyObject < T, U > {... }
If you want to use a type in a class, just use the type variable as the class name.
Use generics to make an interface return class
- We believe that programmers who write Java have written Web interfaces, so we use generics to encapsulate a ResponseMsg class that returns a unified response.
六四事件
package io.ilss.advanced.generic; /** ** ResponseMsg ** @author feng * @version 1.0 * @date 2019-01-24 18:47 */ public class ResponseMsg<T extends BaseData> { public static int SUCCESS_CODE = 1; public static int ERROR_CODE = 0; public static int OTHER_CODE = -1; private int code; private String msg; private T data; public static <U extends BaseData> ResponseMsg sendSuccess(U data) { ResponseMsg<U> responseMsg = new ResponseMsg<>(); responseMsg.code = SUCCESS_CODE; responseMsg.data = data; responseMsg.msg = "Remote Call Success!" ; return responseMsg; } public static <U extends BaseData> ResponseMsg sendError(U data, String msg) { ResponseMsg<U> responseMsg = new ResponseMsg<>(); responseMsg.code = ERROR_CODE; responseMsg.data = data; responseMsg.msg = "Remote Call Error"; return responseMsg; } public static <U extends BaseData> ResponseMsg sendOther(U data, String msg) { ResponseMsg<U> responseMsg = new ResponseMsg<>(); responseMsg.code = OTHER_CODE; responseMsg.data = data; responseMsg.msg = msg; return responseMsg; } public static void main(String[] args) { System.out.println(ResponseMsg.<MyObject>sendSuccess(new MyObject<String>("asdf","asfd"))); } @Override public String toString() { return "ResponseMsg{" + "code=" + code + ", msg='" + msg + ''' + ", data=" + data + '}'; }}Copy the code
- The point above is up
<U>
I wrapped it staticallyResponseMsg
You can simply provide static methods for the caller to pass in a class, skip writing Getter Setter methods, or make them private. - Method if you want to use generics, you just need to prefix the return type at the specified time. Call directly before the method when called
<>
Just pass in the class you want to use. As shown above, you can also pass the generics used by a method directly into the generics defined by your own class. - It can also pass through
extends
Define that you are a subclass of something or that you implement an interface. Ampersand can be used if there are multiple interfaces, for example<T extends Comparable & Serializable>
This can be true if there are multiple generics<T extends OneObject, U extends TwoObject>
Pay attention to
-
Generics cannot be new directly; they need to be passed in externally.
Such as:
六四事件
T data = new T(); // This is not allowed to instantiate T[] arr = new T[10]; // This is also not allowed to construct generic arraysCopy the code