0. Introduction
1. The most simple generic
package io.ilss.advanced.generic;
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);
}
}
复制代码
- MyObject incorporated in a variable T type, with angle brackets
<>
enclose, on the back of the class name. As! When defining a plurality of types may be variable, in <>
a comma-separated e.g. public class MyObject<T, U>{…}
, directly in the class definition of the type of the variable can be regarded as a class name to use if you are using type.
2. Use a generic interface to make a return to class
- I believe writing Java programmers have written Web interface that we use to encapsulate a generic class unified response ResponseMsg returned.
package io.ilss.advanced.generic;
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 +
'}';
}
}
复制代码
- The focus is on the top of
<U>
me with a static method encapsulates the ResponseMsg
building, only need to provide a static method call to let people pass in a class, you can not write Getter Setter method, or directly into private methods.
- If a generic method of use, only when the timing required for the return type of the front can be used together. When invoked directly in front of the method call with
<>
the incoming class you want to use. As indicated above, the method may be passed directly to their generic generic class definition.
- Furthermore it can also
extends
define you are certain subclass or implements an interface. If there are a plurality of interfaces may be connected to & such <T extends Comparable & Serializable>
, if there may be a plurality of generic<T extends OneObject, U extends TwoObject>
3. Note
about me
- Coordinate Hangzhou, ordinary undergraduate reading, computer science and technical expertise, graduation 20 years, is currently in the practice stage.
- Mainly doing Java development, will write about Golang, Shell. Micro services, Big Data interested, ready to do in this direction.
- Is currently in the rookie stage, you chiefs light spray, crazy brother is learning.
- I welcome you and exchange duck! ! !