Generic Java SE 1.5 is the new features, the generic nature of the type of parameters, that is operated by the type of data has been designated as a parameter. This parameter can be used in the type of category, and the interface methods to create, respectively, as the generic category, generic interface, the generic approach.
The introduction of generic Java language has the advantage of simple security.
Prior to the Java SE 1.5, not the generic situation, through the Object type of parameters used to achieve the "arbitrary" and "arbitrary" brought disadvantage is that to do explicit type of mandatory conversion, which Conversion is to require developers to the real parameters can predict the types of cases. The mandatory conversion errors, the compiler may not prompt an error in the running when it unusual that this is a security risk.
Generic benefits at compile time is the type of security check, and all the cast are automatic and implicit, to improve code reuse rate.
Generic use in a number of rules and restrictions:
1, the generic type parameter is the only type of category (including custom categories), is not a simple type.
2, with a corresponding number of generic versions can be (because of the type of parameters is uncertain), different types of generic versions of the examples are not compatible.
3, the type of generic parameters can have more than one.
4, the generic type parameter can use the statement extends, for example, <T extends superclass>. Used to be on "the type of community."
5, the generic type parameter can be wildcard type. For example, Class <?> ClassType = Class.forName (java.lang.String);
There are generic interfaces, methods, and so on, a lot of content, take a kung fu master in order to understand and skilled application. In this paper I have time to write the generic understanding of the two examples (see under the impression written), to achieve the same function, using a generic, not a use, by contrast, can quickly learn to the Generic Applications, the Institute is basically a generic 70% of the Institute.
One example: the use of generic
public class Gen<T> {
private T ob; //定义泛型成员变量
public Gen(T ob) {
this.ob = ob;
}
public T getOb() {
return ob;
}
public void setOb(T ob) {
this.ob = ob;
}
public void showTyep() {
System.out.println("T的实际类型是: " + ob.getClass().getName());
}
}
public class GenDemo {
public static void main(String[] args){
//定义泛型类Gen的一个Integer版本
Gen<Integer> intOb=new Gen<Integer>(88);
intOb.showTyep();
int i= intOb.getOb();
System.out.println("value= " + i);
System.out.println("----------------------------------");
//定义泛型类Gen的一个String版本
Gen<String> strOb=new Gen<String>("Hello Gen!");
strOb.showTyep();
String s=strOb.getOb();
System.out.println("value= " + s);
}
}
Second example: do not use generic
public class Gen2 (
private Object ob; / / definition of a common type of members of the
public Gen2 (Object ob) (
this.ob = ob;
)
public Object getOb () (
return ob;
)
public void setOb (Object ob) (
this.ob = ob;
)
public void showTyep () (
System.out.println ( "T is the actual type:" + ob.getClass (). GetName ());
)
)
public class GenDemo2 (
public static void main (String [] args) (
/ / Define a category of Gen2 version of the Integer
Gen2 intOb = new Gen2 (new Integer (88));
intOb.showTyep ();
int i = (Integer) intOb.getOb ();
System.out.println ( "value =" + i);
System.out.println ("----------------------------------");
/ / Define the type of Gen2 version of a String
Gen2 strOb = new Gen2 ( "Hello Gen!");
strOb.showTyep ();
String s = (String) strOb.getOb ();
System.out.println ( "value =" + s);
)
)
The results:
Demo to run two examples of the results are the same, the console output is as follows:
T is the actual type:
java.lang.Integer
value = 88
----------------------------------
T is the actual type: java.lang.String
value = Hello Gen!
Process finished with exit code 0
To see to understand the future of the basic applications and generic code is not a problem on the reading. |