Flutter学习日记:Dart语言学习之typedef

笔者非大神,学习小白,个人见解,欢迎指正

typedef

In Dart, functions are objects, just like strings and numbers are objects. A typedef, or function-type alias, gives a function type a name that you can use when declaring fields and return types. A typedef retains type information when a function type is assigned to a variable.

typedef给某一种特定的函数类型起了一个名字,可以认为是一个类型的别名,可以类比class和对象这样理解:自己定义了一种数据类型,不过这种数据类型是函数类型,一个一个的具体实现的函数就相当于按照这种类型实例化的对象
会有类型检查

typedef Fly = void Function(int value) ;
void main(){
	Bird bird = Bird((int a){print(a);});//如果实参函数的类型不是该类型的话 编译不通过
	bird.fly(3);
}

class Bird{
	Fly fly;
	Bird(this.fly);
}

Flutter中ValueChanged<T>的定义

typedef ValueChanged<T>= void Function(T value);

猜你喜欢

转载自blog.csdn.net/FreeAndWake/article/details/88979769