java basis - the use of generics

There are generic classes, generic interfaces, generic method generic use.

Means the generic type parameter is also passed as a parameter, (for instance of the class or method call) is used when the incoming type.

Generic class

Parameters passed when instantiating type, can not be used to instancece of generic classes.

public class Generic<T>{
    private T key;

    public Generic(T key) {
        this.key = key;
    }

    public T getKey(){
        return key;
    }
}
View Code
// generic type parameter class type only (including a custom class), can not be a simple type 
the Generic <Integer> = genericInteger new new the Generic <Integer> (123456 ); 
 
// passed argument and the generic type required the same type of parameter types, namely String. 
the generic <String> = genericstring new new the generic <String> ( "key_vlaue" ); 

Log.d ( "generic test", "Key iS" + genericInteger.getKey ()); 
the Log 2.d ( "generic test", "key is" + genericString.getKey ());
View Code

Generic Interface

And use substantially the same generic class, it may not pass parameters to achieve the interface type, parameter types may be passed.

// definition of a generic interface 
public  interface Generator <T> {
     public T Next (); 
}
View Code
/ ** 
 * When the generic argument passed in, and define the same generic class, the class declaration time, need to be added together to the generic class declaration 
 * namely: class FruitGenerator <T> implements Generator < T> { 
 * if you do not declare a generic, such as: class FruitGenerator implements Generator <T> , the compiler will complain: "Unknown class" 
 * / 
class FruitGenerator <T> the implements Generator <T> { 
    @Override 
    public T Next () {
         return  null ; 
    } 
}
View Code
/ ** 
 * When passing the generic argument: 
 * Define a production implements this interface, though we only created a generic interface Generator <T> 
 * However, we can pass an unlimited number of arguments to T, the formation of numerous species Generator type interfaces. 
 * When generic interface implementation classes are implemented as generic type has passed for the argument, where the use of all be replaced by a generic argument types passed 
 * namely: Generator <T>, public T next (); in the T should be replaced by the incoming String. 
 * / 
Public  class FruitGenerator the implements Generator <String> { 

    Private String [] = Fruits new new String [] { "the Apple", "Banana", "Pear" }; 

    @Override 
    public String Next () { 
        the Random RAND = new new the Random () ;
         return Fruits [rand.nextInt (. 3 )]; 
    } 
}
View Code

Generic method

The type of parameters passed when calling

public class ArrayAlg {

    public static <T> T getMiddle(T... a) {
        return a[a.length / 2];
    }
    
    public static void main(String[] args){
        System.out.println(ArrayAlg.getMiddle(1,2,3,4,5));
    }
}
View Code

Generic wildcards

We know that Ingeteris Numbera subclass, while the characteristic sections we have verified Generic<Ingeter>the Generic<Number>fact is a basic type of the same. So the question is, using Generic<Number>as a parameter of the method, the ability to use the Generic<Ingeter>example of incoming it? Similar in logic Generic<Number>and Generic<Ingeter>whether you can have a parent-child relationship as a generic type of it?

In order to clarify this problem, we use Generic<T>the generic class continue to look at the following example:

public void showKeyValue1(Generic<Number> obj){
    Log.d("泛型测试","key value is " + obj.getKey());
}
View Code
The Generic <Integer> = gInteger new new the Generic <Integer> (123 ); 
the Generic <Number The> = gNumber new new the Generic <Number The> (456 ); 

showKeyValue (gNumber); 

// showKeyValue this approach the compiler will complain to us: Generic <java .lang.Integer> 
 // CAN not BE Applied to the Generic <java.lang.Number>
 // showKeyValue (gInteger);
View Code

We can see through the message Generic<Integer>can not be seen as a `Generic<Number>subclass. It can be seen: the same may correspond to a plurality of generic versions (because the parameter type is uncertain), different versions of the generic class instance are not compatible.

Back to the example above, how to solve the above problems? Can not in order to define a new approach to deal with Generic<Integer>the type of class, which is obviously contrary to java in more than one concept. So we need a logically can be expressed simultaneously Generic<Integer>and Generic<Number>the parent class reference type. Thus came into being the type of wildcards.

The above method we can change it:

public void showKeyValue1(Generic<?> obj){
    Log.d("泛型测试","key value is " + obj.getKey());
}
View Code

<?> And <T> difference

? May be compatible with different types, T is only one type.

For example collections ArrayList <?> Inside element of different types can be added simultaneously, ArrayList <T> is added to a first type of element T to determine, only one type is added.

 

---

reference:

https://www.cnblogs.com/coprince/p/8603492.html

https://www.cnblogs.com/diandianquanquan/p/10640781.html

Guess you like

Origin www.cnblogs.com/lankerenf3039/p/12027562.html
Recommended