I can't tell what the generic type is. Here we will introduce a problem:
Defines a coordinate point class and requires that various types of data, such as integer, floating point, and string types, be saved.
Since the variable type is unknown at first, it is easy to think that it is replaced by all types of parent classes, that is, object classes.
No nonsense. UseCodeTo reflect
Example 1: use an object to input uncertain Data Types
// This is the defined coordinate class point {private object X; private object y; // use an object to indicate an uncertain type public point (Object X, object y) {This. setx (x); this. sety (y);} public void setx (Object X) {This. X = x;} public object getx () {return X;} public void sety (Object y) {This. y = y ;}public object Gety () {return y ;}// test class public class demo {public static void main (string [] ARGs) {system. out. println ("using floating point numbers to represent coordinates:"); point P = new point (12.23, 23.21); // here, convert the object class to the double class and Unbox it automatically, the following two types are the same: system. out. println ("X coordinate" + (double) p. getx (); system. out. println ("Y coordinate" + (double) p. gety (); system. out. println (); system. out. println ("using Integers to represent coordinates:"); point P2 = new point (12, 23); system. out. println ("X coordinate" + (integer) p2.getx (); system. out. println ("Y coordinate" + (integer) p2.gety (); system. out. println (); system. out. println ("represents coordinates with strings:"); point P3 = new point ("29 degrees north latitude", "113 degrees east longitude"); system. out. println ("X coordinate" + (string) p3.getx (); system. out. println ("Y coordinate" + (string) p3.gety () ;}
In this way, we can substitute different types of data, but don't forget that the data is still in the object type, that is, all types of parent classes.
You must clearly understand what type you pass in, And then perform downward transformation to use it.
Although this satisfies the needs, it implies an insecure factor. Why is it implicit?
For example, we use new point (12.23, "29 degrees north latitude") to construct a point object.
What will happen if I use double to transform it down?
Yes, the compilation will pass, but once it is run, a type conversion exception will occur.
It is also easy to avoid class conversion exceptions. Replace the object declaration with a fixed type declaration (for example, string X, string y), so that an error will be reported during compilation.
Then you can find the error and modify it.
However, as a result, we cannot meet our needs.
To eliminate security risks and substitute various data types, the norm people introduced the concept of generic in jdk1.5.
Let's take a look at how to rewrite the above Code with generics.
Instance 2: Generic
Class point <t> {// here t is used to indicate the uncertain type private t x; private t y; Public point (t x, t y) {This. setx (x); this. sety (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 ;}} public class demo {public static void main (string [] ARGs) {system. out. println ("using floating point numbers to represent coordinates:"); // after using a generic rewrite, you do not need to perform a downward transformation for data. point <double> P = new point <double> (12.23, 23.21); system. out. println ("X coordinate" + P. getx (); system. out. println ("Y coordinate" + P. gety (); system. out. println (); system. out. println ("using Integers to represent coordinates:"); point <integer> P2 = new point <integer> (12, 23); system. out. println ("X coordinate" + p2.getx (); system. out. println ("Y coordinate" + p2.gety (); system. out. println (); system. out. println ("represents coordinates with strings:"); point <string> P3 = new point <string> ("29 degrees north latitude", "113 degrees east longitude"); system. out. println ("X coordinate" + p3.getx (); system. out. println ("Y coordinate" + p3.gety ());}}
After the generic type is used, the security risks can be reduced.
If we intentionally input different data types at this time:
Point <double> P = new point <double> ("29 degrees north latitude", 12.22 );
Then, an error will be reported during compilation.
Although the generic type is defined, if you do not use the generic mechanism in the constructor, the data will be processed as an object.
The main purpose of doing so is to be compatible with the old code before jdk1.4, such
Point P = new point (22.11, 23.21 );
The final running result is the same, but a warning message is prompted during compilation.
Example 3: Generic Method
As shown in the preceding example, once the object type is specified in the constructor, the same type is used in the entire class.
The most typical example is used in the collection framework, such as arraylist <integer> Al = new arraylist <integer> ();
In this case, all object types operated in Al are integer.
However, sometimes we do not want to fix dead objects, but want to use generic technology more flexibly.
In this case, you can try the generic method.
// The class name is not followed by the generic class print {// define the generic public in the method <t> void print (t) {system. out. println (t);} Public <E> void show (e) {system. out. println (e) ;}} public class demo {public static void main (string [] ARGs) {print P = new print (); p. print (12); p. print ("hello"); p. show (New INTEGER (33); p. show (23 );}}
In this way, there is no big difference with using object objects in methods.
Moreover, after jdk1.5, the Automatic Disassembly box function was added, saving the trouble of downward transformation.
Instance 4: generic interface
// Define a generic interface inter <t> {public void print (T);} // Implementation Method 1: class interdemo1 implements inter <string> {public void print (string t) {system. out. println ("Print:" + T) ;}// Implementation Method 2: Class interdemo2 <t> implements inter <t> {public void print (t) {system. out. println ("Print:" + T) ;}} class demo {public static void main (string [] ARGs) {interdemo1 id1 = new interdemo1 (); id1.print ("hello"); interdemo2 <integer> Id2 = new interdemo2 <integer> (); id2.print (New INTEGER (23 ));}}
You can use either of the following methods to implement a generic interface:
Another method is to still use generics and determine the generics during construction.