在Dart中为类成员定义函数签名

在Dart中为类成员定义函数签名,dart,Dart,我想指定用作类字段的函数的签名。 以下是一个例子: class Space<PointType> { // num distance(PointType, PointType); This does not work final distance; // This works but dystance types are not defined Space(num this.distance(PointType, PointType)); }

我想指定用作类字段的函数的签名。 以下是一个例子:

class Space<PointType>
{
    // num distance(PointType, PointType); This does not work
    final distance; // This works but dystance types are not defined 

    Space(num this.distance(PointType, PointType));     
}
类空间
{
//num distance(PointType,PointType);这不起作用
final distance;//此选项有效,但未定义异常类型
空间(num this.distance(PointType,PointType));
}
我知道我可以使用typedef定义回调接口。然而,这在泛型中似乎不起作用。
有什么建议吗?

您可以使用
typedef
声明类字段中使用的函数的签名。我不完全确定我是否遵循了您的具体示例,因此我将保持讨论的一般性

以下是使用
typedef
的语法:

typedef函数returnType name of typedef(ParamType paramName);
以下是一个具体的例子:

typedef字符串MyFuncType(int x,int y);
本例定义了
MyFuncType
以返回一个
String
,并获取两个
int
参数

class-MyClass{
MyFuncType func;//匹配返回字符串并接受2个int参数的func。
...
}

您可以阅读关于使用
typedef
s的更全面的讨论。

您可以将泛型与
typedef
一起使用。就你而言:

typedef num计算距离(ep1,ep2);
类空间{
最终计算距离;
空间(这个距离);
}