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. Use Code to reflect
Example 1: use an Object to input uncertain Data Types
Copy codeThe Code is as follows: // This is the defined coordinate point class.
Class Point {
Private Object x;
Private Object y;
// Use Object to indicate uncertain types
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 to represent coordinates :");
Point p = new Point (12.23, 23.21 );
// Convert the Object class to the Double class and automatically unpack the box. The 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 ("using string to represent coordinates :");
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
Copy codeThe Code is as follows: 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 to represent coordinates :");
// After rewriting with a generic model, the data does not need to be transformed further.
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 ("using string to represent coordinates :");
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.
Copy codeThe Code is as follows: // The class name is not followed by a generic definition
Class Print {
// Define generics in methods
Public <T> void print (T t ){
System. out. println (t );
}
Public <E> void show (E 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
Copy codeThe Code is as follows: // define a generic interface
Interface Inter <T>
{
Public void print (T t );
}
// Method 1:
Class InterDemo1 implements Inter <String> {
Public void print (String t ){
System. out. println ("print:" + t );
}
}
// Method 2:
Class InterDemo2 <T> implements Inter <T> {
Public void print (T 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.