Dart 泛型在 Flutter 中的应用
通俗理解:泛型主要是解决类、接口、方法的复用性、以及对不特定数据类型的支持。
创建一个泛型类
泛型类主要作用:提高代码的复用度
shell
class Cache<T> {
final Map<String, T> _cached = {};
void setItem(String key, T value) {
_cached[key] = value;
}
///泛型方法
T? getItem(String key) {
return _cached[key];
}
}使用方法
shell
Cache<String> cache1 = Cache();
cache1.setItem("cache1", "cache1 value"); //泛型作用:类型检查约束类比:List<String>
// 获取某个值
String? string1 = cache1.getItem("cache1");
print('string1:$string1');
Cache<int> cache2 = Cache();
cache2.setItem("cache2", 1008);
int? int1 = cache2.getItem("cache2");
print(int1);继承泛型
有时候你在实现类似通用接口的泛型中,期望的类型是某些特定类型时,这时可以使用类型约束
shell
class Member<T extends Person> {
final T _person;
///泛型作用:约束参数类型
Member(this._person);
String fixedName() {
return 'fixed:${_person.name}';
}
}使用方法
shell
Member<Student> member = Member(Student('985', 'jack', 26));
print(member.fixedName());
剑鸣秋朔