The extensive new character that is Java SE 1.5, extensive is parameter spends a kind constitutionally, the data type that operates that is to say is appointed to be a parameter. Type of this kind of parameter can be used in kind, of interface and method in founding, call flood respectively model kind, extensive interface, extensive method.
Java language introduces extensive model advantage is safe and simple.
Before Java SE 1.5, did not float model of the circumstance below, the “ random that realizes parameter through coming to the adduction of type Object changes ” , the weakness that “ random changes ” to bring is to should make explicit compulsive type transition, and this kind of changeover is requirement developer undertake below the circumstance that can foretell to actual parameter type. Change wrong condition to compulsive type, compile implement the likelihood does not hint mistake, just appear when move unusual, this is a safe hidden trouble.
Extensive advantage is type safety is checked when compile, and all compulsive changeover are automatic mix of concealed type, raise the rate of put sb in a very important position of code.
Extensive in use still has a few regulation and restriction:
1, extensive type parameter can be kind of type only (include to be defined oneself kind) , cannot be simple type.
2, same kind of extensive OK and many more corresponding version (because parameter type is doubtful,decide) , the extensive of different version kind example is incompatible.
3, extensive type parameter can have many.
4, extensive parameter type can use Extends statement, for example <T Extends Superclass> . Customarily becomes “ to have group kind ” .
5, extensive parameter type still can be a type matching accord with. For example Class< ? >ClassType = Class.forName(java.lang.String);
Extensive still interface, method is waited a moment, content is very much, need costs kongfu ability one time to understanding masters and apply adroitly. Give out here two case that I once knew extensive moment to draw up (write according to the impression that look) , realize same function, one used extensive model, one is not used, pass contrast, can learn extensive very quickly model application, the society basically learned the extensive content of 70% this.
Example one: Used extensive model
Public Class Gen<T>{
Private T Ob; // defines extensive member variable
Public Gen(T Ob) {
This.ob = Ob;
}
Public T GetOb() {
Return Ob;
}
Public Void SetOb(T Ob) {
This.ob = Ob;
}
Public Void ShowTyep() {
The real kind of System.out.println("T is: " Ob.getClass().getName());
}
}
Public Class GenDemo {
Public Static Void Main(String[] Args){
/ / definition extensive kind an Integer version of Gen
Gen<Integer>IntOb=new Gen<Integer>(88);
Previous12 Next
Hot Tags: