Dart-Functions

Dart-Functions(函数)

/*
Dart 是一种真正的面向对象语言,因此即使是函数也是对象并且具有类型Function
这意味着函数可以分配给变量或作为参数传递给其他函数,您也可以调用Dart类的实例
*/

目录:

  1. define function(定义函数)
  2. ignore return type(省略返回类型的定义)
  3. dynamic return type(动态返回类型的定义)
  4. optional parameter(函数的可选参数)
  5. Required parameters(必选参数)
  6. anonymousFunction(匿名函数)
  7. nestFunction(嵌套函数)
  8. Function type(函数也是一种具有Function类型的对象)
  9. Function Parameter(函数作为另一个函数的参数类型)
  10. Function Returned(函数作为另一个函数的返回类型)

1. define function(定义函数)

/*
Define function.
 */
bool isBool(int a, int b) {
  return a == b;
}

对于只包含一个表达式的函数,可以使用简写语法:

bool isNobel(int a, int b) => a == b;

2. ignore return type(省略返回类型的定义)

/*
ignore the define of return type:bool
 */
isBoolTwo(int a, int b) {
  return a == b;
}

3. dynamic return type(动态返回类型的定义)

/*
this function's return type is dynamic,
is int
is bool
is String
 */
isBoolThree(int a, int b) {
  if (a < b) {
    return a;
  } else if (a == b) {
    return true;
  } else if (a > b) {
    return "min:${b}";
  }
}

4. optional parameter(可选参数的函数)

/*
Optional parameters
可选函数、命名函数
 */
isBoolFour({int a, int b}) {
  if (a < b) {
    return a;
  } else if (a == b) {
    return true;
  } else if (a > b) {
    return "min:${b}";
  }
}


  //调用可选/命名参数时,传递的参数的
  print("optional parameters:${isBoolFour(b: 1, a: 2)}");

一组函数参数
[]

dynamic functionArray(String name, [String arguments]) {
  return arguments?.length;
}

5. Required parameters(必选参数)

@required

/*
Required parameters
 */
isBoolFive({int a = 0, @required int b}) {
  if (a == null) {
    return b;
  } else {
    return a + b;
  }
}


  //调用带有必须参数的,命名函数
  print("required parameters:${isBoolFive(b: 5)}");

6. anonymousFunction(匿名函数)

/*
Define anonymous function
 */
anonymousFunction() {
  print("");
  print("Anonymous function.");
  var list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

  list.forEach((item) {
    print("item:${item}");
  });
}

7. nestFunction(嵌套函数)

bool topLevel = true;

void nestTopLevelFunction() {
  var insideMain = true;

  void myFunction() {
    var insideFunction = true;

    void nestedFunction() {
      var insideNestedFunction = true;

      print(topLevel);
      print(insideMain);
      print(insideFunction);
      print(insideNestedFunction);
    }

    nestedFunction();
  }

  myFunction();
}

8. Function type(函数也是一种具有Function类型的对象)

void functionType(num a) {
  num a;
  int b;
  double c;
  String d;
  bool e;
  Function f;

}

9. Function Parameter(函数作为另一个函数的参数类型)

函数作为另一个函数的参数有两种定义方式

  1. 使用Function
void two(Function fun) {
    fun("James");
}
  1. 使用void lambda(String name, int age)
void twoTwo(void lambda(String name, int age)) {
    lambda("Air_twoTwo", 10);
}

10. Function Returned(函数作为另一个函数的返回类型)

函数作为另一个函数的返回类型有两种方式

  1. 使用 Function
Function four(String name) {
    Function fun = () {
    return "$name";
    };

    return fun;
}
  1. 使用 typedef + Function
typedef Callback = void Function(int count, int total);

    ///定义返回函数的函数
    Callback fourFour(String name){
        var call = (int count ,int total){
            print("fourFour: $name , $count , $total");
        };
        return call;
    }

    ///....
        ///调用
        fourFour("Air")(10, 100);

未完待续。。。


   转载规则


《Dart-Functions》 Air 采用 知识共享署名 4.0 国际许可协议 进行许可。
  目录