T getData<T>(T value) {
return value;
}
void main() {
print(getData<int>(21));
}
class MyList<T>{
List list = <T>[];
void add(T value) {
this.list.add(value);
}
List getList(){
return list;
}
}
void main() {
MyList l1 = new MyList<String>();
l1.add('zs');
l1.add('ww');
print(l1.getList());
}
- 定义一个泛型接口,约束它的子类必须有getByKey和setBykey
- 要求setByKey时value的类型和实例化子类的时候指定的类型一致
abstract class Cache<T> {
getByKey(String key);
void setByKey(String key, T value);
}
class FileCache<T> implements Cache<T> {
@override
getByKey(String key) {
return key;
}
@override
void setByKey(String key, T value) {
print('${key} ==== ${value}');
}
}
void main() {
FileCache f = new FileCache<String>();
f.setByKey('index', '首页数据');
}