Believe write Java, mostly used the List of implementation class ArrayList, before Java generics, its interior is an array of Object, it also causes a problem, every time when using the element inside need downward transition, and obviously, if the Object is, means we can throw any objects, automatic transformation into Object, so it is easy to a problem when using, don't know what is inside of, such as:
ArrayList list=new ArrayList ();
List. The add (" string1 ");
List. The add (" string2 ");
String STR=(String) in the list. The get (0);
List. The add (new File (" test. TXT "));
Use generic easy, however, we often use the List of generics, but if we want to write a generic class is not so easy,
The most simple generic
Package IO. Ilss. The generic;
Public class MyObject
Private T valueOne;
Private T valueTwo;
Public MyObject (T valueOne, T valueTwo) {
Enclosing valueOne=valueOne;
Enclosing valueTwo=valueTwo;
}
Public T getValueOne () {
Return valueOne;
}
Public void setValueOne valueOne (T) {
Enclosing valueOne=valueOne;
}
Public T getValueTwo () {
Return valueTwo;
}
Public void setValueTwo (T valueTwo) {
Enclosing valueTwo=valueTwo;
}
Public static void main (String [] args) {
MyObject
System. Out. Println (" value one "+ object, valueOne +" value two "+ object, valueTwo);
}
}
In introducing a type variable T MyObject, with Angle brackets & lt;> Enclosed, put in the back of the class name, as above! At the time of definition can have multiple types of variables, in & lt;> With a comma, separated such as public class MyObject
Made a generic interface to return to class
Believe writing Java programmers have written a Web interface, then we use the generic class to encapsulate a unified response returned ResponseMsg,
Package IO. Ilss. The generic;
Public class ResponseMsg
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 & lt; U extends BaseData> ResponseMsg sendSuccess (U data) {
ResponseMsg ResponseMsg=new ResponseMsg<> (a);
ResponseMsg. Code=SUCCESS_CODE;
ResponseMsg. Data=https://bbs.csdn.net/topics/data;
ResponseMsg. MSG="Remote Call Success!" ;
Return responseMsg;
}
Public static & lt; U extends BaseData> ResponseMsg sendError (U data, String MSG) {
ResponseMsg ResponseMsg=new ResponseMsg<> (a);
ResponseMsg. Code=ERROR_CODE;
ResponseMsg. Data=https://bbs.csdn.net/topics/data;
ResponseMsg. MSG="Remote Call Error";
Return responseMsg;
}
Public static & lt; U extends BaseData> ResponseMsg sendOther (U data, String MSG) {
ResponseMsg ResponseMsg=new ResponseMsg<> (a);
ResponseMsg. Code=OTHER_CODE;
ResponseMsg. Data=https://bbs.csdn.net/topics/data;
ResponseMsg. MSG=MSG;
Return responseMsg;
}
Public static void main (String [] args) {
System. Out.println (ResponseMsg. & lt; MyObject> SendSuccess (new MyObject
}
Public String toString () {
Return ResponseMsg {" + "
"Code=" + code +
", MSG='" + MSG +' \ ' '+
, "data=https://bbs.csdn.net/topics/+ data +"
'} ';
}
}
The focus is on & lt; U> I used a static method encapsulates the ResponseMsg builds, only need to provide a static method make calls into a class, also can need not to write Getter Setter method, or directly into a private method,
Method if you want to use generics, need only in front of the return type when timing plus & lt; T> Can be used to invoke directly in front of the method is called when using & lt;> Introduced to you want to use the class can, as shown above, also can method is to use a generic directly into the class definition of generics,
In addition can also extends through limit you are XXX class subclass, or implements an interface, if there are multiple interfaces can be used & amp; Connection, such as & lt; T extends Comparable & amp; Serializable> , if there are multiple generics can & lt; T extends OneObject, U extends TwoObject>
Generic cannot directly new, need external incoming,
Such as:
T data=https://bbs.csdn.net/topics/new (T);//it is not allowed, and cannot be instantiated object
Arr=new T T [] [10];//this is not allowed cannot construct generic array