继承、实现接口、泛型

实现接口:
abstract class A{
  printA();
}
abstract class B{
  printB();
}

class AA implements A , B{
  @override
  printA() {
    print("AAA");
  }

  @override
  printB() {
    print("BBB");
  }

}

如果要集成多个类,用mixins,关键字mixins

class A{
  String info = "this is A";
  printA(){
    print("this is A");
  }
}
class B{
  printB(){
    print("this is B");
  }
}

class C with A , B{
 //注意 A类B类不能继承其他类、且不能有构造函数
}

void main() {
  C c = new C();
  print(c.info);
  c.printA();
  c.printB();
}

泛型

class C with A , B{

  String getData1(String str){
    return str;
  }


  int getData2(int str){
    return str;
  }
  
  T getData<T>(T value){
    return value;
  }
}

void main() {
  C c = new C();
  c.getData<String>("Hello World");
  c.getData<int>(12);
}
class MyList<T>{
  List list = <T>[];
  void add(T value){
    this.list.add(value);
  }

  List getList(){
    return this.list;
  }
}

void main() {
  MyList li = MyList();//这样初始化可以传入不同类型参数
  li.add(17);
  li.add(16);
  li.add(18);
  li.add("zhangsan");
  li.add("wangwu");
  MyList lii = MyList<String>();//这样初始化,只能传入固定类型数据
  lii.add("zhangsan");
  lii.add("wangwu");
}
abstract class Catch<T>{
  getByKey(String key);
  void setByKey(String key, T value);
}
class FileCatch<T> implements Catch<T>{
  @override
  getByKey(String key) {
    return null;
  }

  @override
  void setByKey(String key, T value) {
    print("我是内存缓存,key=${key}===value=${value}");
  }
}
FileCatch fileCatch = new FileCatch<String>();
fileCatch.setByKey("index", "我是数据");

猜你喜欢

转载自blog.csdn.net/qwildwolf/article/details/117961692