Fultter Study Notes_Dart: Generics

What are generics?
Popular understanding: Generics solve the reusability of classes, interfaces and methods, and support for unspecific data types (type verification)

generic method

只能返回string类型的数据

  String getData(String value){
    
    
      return value;
  }

Here is the normal method;

  String getData1(String value){
    
    
      return value;
  }

  int getData2(int value){
    
    
      return value;
  }

This method can support returning string type and int type at the same time, but it will cause code redundancy;

  getData(value){
    
    
      return value;
  }

Not specifying a type solves this problem, but forgoes type checking.

What we want to achieve is what type is passed in and what type is returned.
For example: if you pass in a number type, you must return a number type; if you pass in a string type, you must return a string type. The generic method can solve this problem:

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


void main(){
    
    
    print(getData<int>(12));
}

This is the format of the generic method. Whatever type we need, we specify the type in <>, which not only realizes the reusability of the code, but also takes into account the type checking

Of course, if you don't want to verify the return type, you can also write it like this:

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

generic class

Usage of collection List generic class

Case: convert the following class into a generic class, and require MyList to add int type data or String type data. But the type added for each call should be unified

class MyList {
    
    
  List list = <int>[];
  void add(int value) {
    
    
    this.list.add(value);
  }
  List getList() {
    
    
    return list;
  }
}


MyList l = new MyList();
l.add(1);
l.add(12);
l.add(5);
print(l.getList());

Converted to a generic class as follows:

class MyList<T> {
    
    
  List list = <T>[];
  void add(T value) {
    
    
    this.list.add(value);
  }

  List getList() {
    
    
    return list;
  }
}


//泛型类在调用的时候可以不指定泛型类型,不指定泛型类型则可以传入任意类型
  MyList l1=new MyList();
  l1.add("张三");
  l1.add(12);
  l1.add(true);
  print(l1.getList());


  MyList l2 = new MyList<String>();
  l2.add("张三1");
  // l2.add(11);  //错误的写法,此时不能传入int类型
  print(l2.getList());


  MyList l3 = new MyList<int>();
  l3.add(11);
  l3.add(12);
  //l3.add("aaaa");错误的写法,此时不能传入string类型
  print(l3.getList());

generic interface

Generic Interfaces in Dart

The following is an example given by the official documentation:

  abstract class ObjectCache {
    
    
    getByKey(String key);
    void setByKey(String key, Object value);
  }

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


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

Case:
It is required to implement the function of data caching: there are file caching and memory caching. Memory cache and file cache are implemented according to interface constraints.

1、定义一个泛型接口 约束实现它的子类必须有getByKey(key) 和 setByKey(key,value)

2、要求setByKey的时候的value的类型和实例化子类的时候指定的类型一致

First define the 'cache' interface:

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

Next, define the classes of file cache and memory cache respectively to implement the interface:

//文件缓存
class FlieCache<T> implements Cache<T>{
    
    
  @override
  getByKey(String key) {
    
        
    return null;
  }

  @override
  void setByKey(String key, T value) {
    
    
   print("我是文件缓存 把key=${key}  value=${value}的数据写入到了文件中");
  }
}

//内存缓存
class MemoryCache<T> implements Cache<T>{
    
    
  @override
  getByKey(String key) {
    
       
    return null;
  }

  @override
  void setByKey(String key, T value) {
    
    
       print("我是内存缓存 把key=${key}  value=${value} -写入到了内存中");
  }
}



void main(){
    
    
    // MemoryCache m=new MemoryCache<String>();
    //  m.setByKey('index', '首页数据');

     MemoryCache m=new MemoryCache<Map>();
     m.setByKey('index', {
    
    "name":"张三","age":20});
}

Guess you like

Origin blog.csdn.net/weixin_46136019/article/details/128908778