8.Generics 泛型(Dart中文文档)

  • 这篇翻译的不好

如果你看API文档中的数组篇,你会发现类型一般写成List.<...>的写法表示通用类型的数组(未明确指定数组中的数据类型)。通常情况泛型类型用E,T,S,K,V表示。

Why use generics? 为什么用泛型

泛型是类型安全的(意思是你必须指定数据的类型),但是它的写法比硬编码指定类型高效的多:

  • Properly specifying generic types results in better generated code.
  • 减少重复代码

如果你想让数组只有String值,定义为List。这样,后续代码中,如果给数组赋值了非String类型,编译器将提示报错,

var names = List<String>();
names.addAll(['Seth', 'Kathy', 'Lars']);
names.add(42); // Error

另一个使用泛型是原因是为了减少重复代码。泛型让你通过一个单一类可以适配多种类型的数据操作,同时可以进行静态代码检查(比如,类型安全检查)。

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

上面代码是对Object类型操作,在没用泛型的情况下,你想对String类型操作,就得重新定义一个类

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

后面,你如果相对num类型操作,还得重新定义一个类。

而泛型就可以解决上面的问题,它通过对类型参数化,实现一个类针对多种数据类型操作的适配。

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

Using collection literals 使用集合

List和map的字面量

List和map的字面量方式可以用指定类型参数。

var names = <String>['Seth', 'Kathy', 'Lars'];
var pages = <String, String>{
  'index.html': 'Homepage',
  'robots.txt': 'Hints for web robots',
  'humans.txt': 'We are people, not machines'
};

Using parameterized types with constructors 构造器中使用类型参数

var names = List<String>();
names.addAll(['Seth', 'Kathy', 'Lars']);
var nameSet = Set<String>.from(names);

下面是map的泛型构造写法:

var views = Map<int, View>();

Generic collections and the types they contain

Dart的泛型类型是在运行时绑定的,这样,在运行时,可以知道List内具体类型。

var names = List<String>();
names.addAll(['Seth', 'Kathy', 'Lars']);
print(names is List<String>); // true

注意:java中,泛型是采用擦除的方式,它在运行时,其实对象都是Object类型或者泛型的约束父类。

Restricting the parameterized type

当泛型时,希望限制参数的类型范围,可以用extends关键字约束。

class Foo<T extends SomeBaseClass> {
  // Implementation goes here...
  String toString() => "Instance of 'Foo<$T>'";
}

class Extender extends SomeBaseClass {...}

添加约束后,只要是指定的父类或者其子类都可以作为泛型参数。

var someBaseClassFoo = Foo<SomeBaseClass>();
var extenderFoo = Foo<Extender>();

也可以不指定泛型参数。

var foo = Foo();
print(foo); // Instance of 'Foo<SomeBaseClass>'

不可以用限定范围的泛型参数,这样静态代码检查器将提示错误。

var foo = Foo<Object>();

Using generic methods 泛型方法

Initially, Dart’s generic support was limited to classes. A newer syntax, called generic methods, allows type arguments on methods and functions:

T first<T>(List<T> ts) {
  // Do some initial work or error checking, then...
  T tmp = ts[0];
  // Do some additional checking or processing...
  return tmp;
}

下面是允许使用泛型方法的场景:

In the function’s return type (T).
In the type of an argument (List).
In the type of a local variable (T tmp).

posted @ 2019-01-04 09:03  lowezheng  阅读(1078)  评论(0编辑  收藏  举报