java generics, a brief introduction. Use of the modified method

A, <R> (R r) of all types can be stored default object. This capital letter R is arbitrarily defined, to maintain consistency before and after!

Package com.aaa.test;
 / * 
 * demonstrates a method of modifying a generic method Oh 
 * effect of increasing code reuse method of reducing the use of heavy-duty 
 * real column 
 * 
 * / 

Import  static org.junit.Assert *. ; 

Import org.junit.Test; 

Import com.aaa.entity.People;
 Import com.aaa.entity.User; 

public  class Demo03 {
     // definition of a <W> is the list of parameters (W w) before the method run01
     // W is the default type is object value can put various types of 
    public <W is> void run01 (W is w) { 
        System.out.println ( "w run01 value performs the method of:" + w); 
    } 
    
    
    public <R & lt> voidrun02 (R & lt R & lt) { 
        System.out.println ( "generic experience strong and fast ----" + R & lt); 
    } 
    
    public  static  void main (String [] args) { 
        Demo03 D = new new Demo03 (); 
        
        / / written value is written to the value of w is w equals a target object. 
        d.run01 ( "ha" ); 
        d.run01 ( 456 ); 
        d.run01 ( 'C' ); 
        
        the User U = new new the User (2, "Ma Feifei" ); 
        d.run01 (U); 
        
        
    } 
    
    @Test 
    public  void TestName () throws Exception { 
        Demo03 D= New new Demo03 (); 
        d.run02 ( "default generic object types can put various types of data" ); 
        d.run02 ( 666 ); 
        
    
    } 
    @Test 
    public  void Test01 () throws Exception {
         // already known Pan the default type is Object object if it can be defined entity classes I use it on the inside? 
        D = Demo03 new new Demo03 (); 
        
        // define the user id and name in 
        the User U = new new the User (1, "Lu Wei regressed this!" ); 
        D.run01 (U);      // Why do I demofanxing that is? w is the value of the user at this time the object returned tostring user is user entity class object. 
        
        
        The p-People = new newPeople (2, "SADF" ); 
        d.run01 (P);    // W value 
        
    } 
}

Second, a plurality of generic definitions

Package com.aaa.test; 

Import  static org.junit.Assert *. ; 

Import org.junit.Test; 

Import com.aaa.entity.User; 

/ * 
 * define multiple presentations in generics 
 * defining a plurality of parameters when separated by commas. 
 * 
 * Similarly, the generic default value is defined herein Object 
 * 
 * / 
public  class Demo06 {
     public <S, B> void Test01 (S s, B B) { 
        System.out.println ( "value of s is defined : "+ s +" ---- value of b is defined by: "+ b); 
    } 
    public  static  void main (String [] args) { 
        Demo06 D = new new Demo06 ();
        
        d.test01 ( "Hello", "I am Asahi maple" ); 
        d.test01 ( 66, 999 );         
        
    } 
    @Test 
    public  void TestName () throws Exception { 
        Demo06 D = new new Demo06 ();    
         // where object 
        User = U1 new new the User (. 1, "generic" ); 
        the User U2 = new new the User (. 1, "the definition of two" ); 
        
        d.test01 (U1, U2); 
        
        d.test01 (U1, "mixed with use" ); 
    } 
}

Third, the wildcard parameter constraints.

Package Penalty for com.aaa.test; 

Import java.util.ArrayList;
 Import java.util.List; 

Import com.aaa.entity.User; 

/ * 
 * demo generics wildcards. 
 * List <?> Lists are defined in the list?  
 * 
 * The following references want to define what type of data is defined what, in list 
 * After the data types are defined in the list, the latter is fixed. 
 * 
 * / 
Public  class Demo07 { 

    public  void Test01 (List <?> List) { 
        System.out.println ( "set length of the output list01 ----" + list.size ()); 
    } 
    public  static  void main (String [] args) { 
        Demo07 D = new newDemo07 (); 
        
        // Create a list of data references set to define the type wildcard
         // the user prior on here. 
        List <the User> list01 = new new the ArrayList <> (); 
        
        // User values into 
        the User U = new new the User (. 1, "ha" ); 
        the User U2 = new new the User (2, "hee" );
         // add to list set 
        list01.add (U); 
        
        list01.add (U2); 
        
        d.test01 (list01); 
        
        
        // data collection convenience list01 
        for (the User User: list01) { 
            System.out.println (User); 
        } 
    } 
}


Package com.aaa.test; 

Import the java.util.HashMap;
 Import a java.util.Map; 

public  class Demo08 {
     public  void Test01 (the Map <?,?> map) { 
        System.out.println ( "length of the map - - "+ map.size ()); 
    } 
    
    public  static  void main (String [] args) { 
        Demo08 D = new new Demo08 (); 
        
        
        // a wildcard defined data types Integer, String 
        the Map <Integer, String> = map01 new new the HashMap <> (); 
        
        // here can only according to the type integer and string to add data. 
         map01.put (1, "lubenwei");
         map01.put(2, "马飞飞");
         
         System.out.println(map01);
        
    
        
    }

}

Fourth, the wildcard parameter constraints:? Extends class contains all object types that itself and its subclasses

 

Package com.aaa.test; 

Import  static org.junit.Assert *. ; 

Import of java.util.ArrayList;
 Import java.util.List; 


Import com.aaa.entity.People;
 Import com.aaa.entity.User;
 Import com.aaa.entity.ZiUser; 


/ * 
 * wildcard parameter constraints 
 * extends user subclass ziUser this time user can use?. 
 *? super User empathy user parent class people can use. 
 *   
 *? All objects extends E and the current class of his parent class can be used. 
 * 
 * Subclasses of the parent class 
 * 
 * / 
public  class Demo09 <E> { 
    
    public  void Test01 (List <? The extendsThe User> List) { 
        System.out.println ( "object types include all the extends his class and subclass" + List); 
    } 
    
    // subclass to people with super user after the user objects can also be used. 
    public  void Test02 (List <? super the User> List) { 
        System.out.println ( "super all types of objects comprising such a parent and his" + List); 
    } 
    

    public  void TEST03 (List <? the extends E> List ) { 
        System.out.println ( "? E contains the extends all the object types and his parent class of" + List); 
    } 
    public  static  void main (String [] args) { 
        
        Demo09 <Object> = Dnew new Demo09 <> (); 
        
        // subclass ziUser user is used here because it extends! 
        List <ZiUser> = list0101 new new the ArrayList <> (); 
        List <the User> list01 = new new the ArrayList <> (); 
        
        // add data to 
        the User U1 = new new the User (. 5, "hee" ); 
        list0101.add ( new new ZiUser (2, "Hello" )); 

        // index User 
        list01.add (0 , U1); 
    
        d.test01 (list0101); 
        d.test01 (list01); 

        System.out.println ( "------ --------------- " );
         //The user parent people go into 
        List <People> list02 = new new the ArrayList <> (); 
        List <the User> = list0201 new new the ArrayList <> (); 
        list0201.add ( new new the User (2, "followed by super, user's parent people can also use " )); 
        list02.add ( new new people (. 5," class people " )); 
        d.test02 (list02); 
        d.test02 (list0201); 
         
        
         
    } 
       
}

 

Guess you like

Origin www.cnblogs.com/ZXF6/p/11128744.html