3

クラスフィールドとして使用される関数の署名を指定したいと思います。以下に例を示します。

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));     
}

typedef を使用してコールバック インターフェイスを定義できることはわかっています。ただし、これはジェネリック内では機能しないようです。助言がありますか?

4

2 に答える 2

4

でジェネリックを使用できますtypedef。あなたの場合:

typedef num ComputeDistance<E>(E p1, E p2);
class Space<PointType> {
  final ComputeDistance<PointType> distance;
  Space(this.distance);
}
于 2013-09-07T16:01:36.070 に答える
2

typedefを使用して、クラス フィールドで使用される関数のシグネチャを宣言できます。あなたの特定の例に従うかどうかは完全にはわかりませんので、議論は一般的なものにしておきます。

を使用するための構文は次のとおりですtypedef

typedef functionReturnType nameOfTypedef(ParamType paramName);

具体的な例を次に示します。

typedef String MyFuncType(int x, int y);

この例ではMyFuncType、a を返し、 2 つの引数Stringを取るように定義しています。int

class MyClass {
  MyFuncType func; // Matches a func that returns a String and take 2 int arguments.
  ...
}

https://github.com/dart-lang/cookbook/blob/basics/basics.asciidoc#using-typedef-to-declare-a-function-signaturetypedefで s の使用に関する詳細な議論を読むことができます。

于 2013-09-07T15:53:36.620 に答える