Dart Generics

/ * 

Popular understanding: Generic interface method is to solve the reusability class, and do not support a specific data type (Type check) 

* / 


// only return data type string 

  // String getData (String value) { 
  return value //; 
  //} 
  

// returns string support, and an int type (redundant code) 


  // getData1 String (String value) { 
  // return value; 
  //} 

  // int getData2 (int value) { 
  / / return value; 
  //} 



// returns string while the number and type type is not specified type can solve the problem 


  // the getData (value) { 
  // return value; 
  //} 





// do not specify the type of abandoned type checking. We now want to achieve what the incoming return. For example: Type number passed passed string type number must return type must return string type 
 
  // the getData T <T> (T value) { 
  // return value; 
  //} 

  the getData<T>(T value){
      return value;
  }

void main(){

    // print(getData(21));

    // print(getData('xxx'));

    // getData<String>('你好');

    print(getData<int>(12));

}
// set of usage List generic class 

// Case: convert generic class into the following categories, which can increase the required List int data type, can also increase the String data type. However, the increase of each call type to a unified 

// class PrintClass { 
// new new = List List List <int> (); 
// void the Add (value int) { 
// this.list.add (value); 
//} 
void printInfo // () {           
// for (var I = 0; I <this.list.length; I ++) { 
// Print (This.List [I]); 
//} 
//} 
//} 

// P = new new PrintClass PrintClass (); 
// p.add (. 1); 
// p.add (12 is); 
// p.add (. 5); 
// p.printInfo (); 

 class PrintClass <T> {
       List  List = new new List<T>();
      void add(T value){
          this.list.add(value);
      }
      void printInfo(){          
          for(var i=0;i<this.list.length;i++){
            print(this.list[i]);
          }
      }
 }

main() {  
    // PrintClass p=new PrintClass();
    // p.add(11);
    // p.add('xxx');
    // p.add(5);
    // p.printInfo();

  // PrintClass p=new PrintClass<String>();

  // p.add('你好');

  //  p.add('哈哈');

  P.printInfo // (); 


  PrintClass P = new new PrintClass <int> (); 

  P .add (12 is ); 

   P .add (23 is ); 

  P . PrintInfo (); 


 
  // List List List new new = (); 
  / / List.add (12 is); 
  // List.add ( "Hello"); 
  // Print (List); 



  // = new new List List List <String> (); 

  // // List.add (12 is) ; // error writing 

  // list.add ( 'Hello'); 
  // list.add ( 'Hello. 1'); 

  // Print (List); 

  // = new new List List List <int> () ; 

  // // List.add ( "Hello"); // error writing 
  // List.add (12 is); 

  // Print (List); 

}
/ * 
Dart in generic interface: 

    for data caching function: file cache and memory cache. Memory caching and file caching implemented in the interface constraints. 
    1, a generic definition of interface constraint achieve its subclasses must have getByKey (key) and setByKey (key, value) 
    is consistent when the value of 2, the required setByKey types and instances of subclasses of the type specified time 

* / 

  / / abstract class the ObjectCache { 
  // getByKey (String Key); 
  // void setByKey (String Key, Object value); 
  //} 

  // abstract class StringCache { 
  // getByKey (String Key); 
  // void setByKey (String Key, value String); 
  //} 


  // the Cache abstract class <T> { 
  // getByKey (String Key); 
  // void setByKey (String Key, T value); 
  //} 


abstract  class the Cache <T> { 
  getByKey (String  Key ); 
  void setByKey ( String  Key , T value); 
} 

class FlieCache <T> the implements the Cache <T> { 
  @override 
  getByKey ( String  Key ) {    
     return  null ; 
  } 

  @override 
  void setByKey ( String  Key , T value) {
    Print ( "I am the file cache key = $ {key} value = $ {value} is written into the data file" ); 
  } 
} 

class the MemoryCache <T> the implements the cache <T> {  
  @override
  getByKey ( String Key ) {   
     return  null ; 
  } 

  @override 
  void setByKey ( String  Key , T value) {
        Print ( "I cache to the memory key = $ {key} value = $ {value} - written into the memory" ); 
  } 
} 
main void () { 

    // the MemoryCache the MemoryCache new new = m <String> (); 

    // m.setByKey ( 'index', 'Home data'); 

     the MemoryCache m = new new the MemoryCache <the Map> (); 

     m .setByKey ( ' index ', { "name": " Joe Smith", "Age": 20 is }); 
}

 

Guess you like

Origin www.cnblogs.com/loaderman/p/11026889.html