Good programmers of Java Java Tutorial Share resolve difficulties generics

  Good programmers to share parsing of Java Java Tutorial generic difficulties, provided with <> class or interface, or interface with all belonging to the class type parameters, when using these class or interface to be passed to the <> in a particular the reference data types.

  Generic Technology: In fact, the application at compile time, the compiler technology is to use, to the running time, the generic would not exist.

  Why? Because generics erased: that is, the editor checked the right generic type, in the generated class file is not generic.

  At run time, you know how to get the element type without strong turn it?

  Generics compensation: Because when stored types have been identified are the same types of elements, so that at run time, just to get the type of the element, a conversion can be performed in-house, so users do not have to do the conversion action a.

  When a generic class it?

  Uncertain time references data types, previously used in operations when the class Object to be extended can now be represented by the generic. This avoids strong turn of trouble, and will be transferred to the operational problems compile time.

  Generic defined in the program reflects:

  // generic class: the generic definitions in the class.

  classTool<Q>{

  privateQobj;

  publicvoidsetObject (Qobj) {

  this.obj=obj;

  }

  publicQgetObject () {

  returnobj;

  }

  }

  // When the method of operation of the reference data type uncertain, they can be defined on a generic method.

  public<W>voidmethod(Ww){

  System.out.println("method:"+w);

  }

  // static methods on generic: static methods can not access the class definition of generics. Cited uncertain data type, you must want to define generic in approach if the static method of operation.

  publicstatic<Q>voidfunction(Qt){

  System.out.println("function:"+t);

  }

  // generic interface.

  interfaceInter<T>{

  voidshow(Tt);

  }

  classInterImpl<R>implementsInter<R>{

  publicvoidshow(Rr){

  System.out.println("show:"+r);

  }

  }

  ------------------------------------------------------------

  Generics wildcards: When can solve the specific type of uncertainty, this is a wildcard; when the type of operation, it is not necessary to use a specific type of function, only the function Object class?. You can use? Wildcards to table an unknown type.

  Generics Limited:

  Limit:? extendsE: of type E or E may receive subtype objects.

  Lower:? superE: of type E or E may receive a parent type object.

  When the upper limit of use: When you add an element to the collection, you can either add an object of type E, and can add a sub-type object E. why? Since the time taken, type E E class object may be received, and may receive the object E subtype.

  When a lower limit: when the element acquired from the collection operation may be the type of the current receiving element may also be received by the parent type of the current element.

  Generics details:

  1), on behalf of generics in the end it depends on what type passed by the caller type, if not pass, the default type Object;

  2), with the use of a generic class object is created on both sides of the equation specified generic must be consistent;

  The reason: just look at the variable object calls the method when checking compiler, however, we must consider the specific type of the object when calling the method during the run;

  3), on both sides of the equation can be used on either side of generic, while not in use (for backward compatibility) in the other;

  ArrayList<String>al=newArrayList<Object>();//错

  // to ensure that the specific type of generic agreement on both sides of it, so not prone to error.

  ArrayList<?extendsObject>al=newArrayList<String>();

  al.add("aa");//错

  // because the collection can store specific object String, can also be stored in other subclasses of Object, it is not appropriate to add specific types of objects, type checking security problems. ? extendsObject representatives of Object subtype uncertain, how you can add specific types of objects it?

  publicstaticvoidmethod(ArrayList<?extendsObject>al){

  al.add("abc");//错

  // Object class method can only be called on al elements of the collection, specific sub-type of approach does not work, because the sub-type of uncertainty.

  }

Guess you like

Origin blog.51cto.com/14573321/2447039