• Dart(9)-函数


    一、函数定义

    (1) 函数的返回类型可以省略 (不建议省略)。若省略,DartVM默认会在函数内的最后一行加上return null。

    (2) 函数都有返回类型,void类型函数实际返回null

    (3) 函数可以在函数内部定义,无限嵌套

    (4) 支持缩写语法=>

    (5) 支持可选命名参数

    (6) 支持可选位置参数

    (7) 支持闭包

    (8) 支持匿名函数

    (9)支持typedef关键字

    (10) 支持顶层函数和局部函数,

    二、函数的返回类型可以省略

    1. void main() {
    2. }
    3. class Clazz {
    4. func() {
    5. return null; //若不写,DartVM会默认加上
    6. }
    7. }

    三、函数内定义函数

    1. void main() {
    2.  int defineFun() {
    3.    return 1+1;
    4. }
    5.  
    6. }//defineFun函数只能在main函数内部调用

    四、支持缩写语法

    当函数的{}内只有一行代码时,可以使用=>代替{},比如:

    1. void todo() {
    2.  print("Hello");
    3. }
    4. void todo2() => print("Hello");
    5. int todo3(int a, int b) => a+b;

    五、可选命名参数

    1. int optionalNamedFun({int a=0, int b=0, int c}) {
    2.  return a+b+c;
    3. }
    4. var x = optionalNamedFun(a:2,b:3);
    5. print(x); //5

    比如,上面的代码中c没有赋初始值,就会报下面的错,所以一定要记得在运算时赋初始值,不然DartMV会给null

    lib/dartEx2.dart: Warning: Interpreting this as package URI, 'package:flutter_demo/dartEx2.dart'.
    Unhandled exception:
    NoSuchMethodError: The method '_addFromInteger' was called on null.
    Receiver: null
    Tried calling: _addFromInteger(5)
    #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
    #1      int.+ (dart:core-patch/integers.dart:10:38)
    #2      optionalNamedFun (package:flutter_demo/dartEx2.dart:19:13)
    #3      main (package:flutter_demo/dartEx2.dart:4:11)
    #4      _startIsolate. (dart:isolate-patch/isolate_patch.dart:301:19)
    #5      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)

    也可以这样写,固定参数+可选参数

    1. int optionalNamedFun(int z,{int a=0, int b=0, int c=0}) {
    2.  return z+a+b+c;
    3. }
    4. var x = optionalNamedFun(1,a:2,b:3);
    5. print(x); //6

    六、可选位置参数

    把可选参数放到 [] 中,必填参数要放在可选参数前面

    1. int optionalLocationFun(int z,[int a=0, int b=0, int c=0]) {
    2.  return z+a+b+c;
    3. }
    4. var x = optionalLocationFun(1,2); //这里的2是给a赋值。
    5. var y = optionalLocationFun(1); //这里的1是给z赋值。
    6. print(x);
    7. print(y);

    Ps: 如果在参数中,定义的是集合,需要注意如下,必须使用const进行修饰

    1. int optionalCollection({List list = const[1,2,3]}) {
    2. }

    七、支持闭包

    一个可以使用另外一个函数作用域中的变量的函数。

    1. Function closureFun(int a) {
    2.    return (int b)=> a+b;
    3. }
    4. var operationFun =  closureFun(20); // a=20
    5. print(operationFun(25)); // b=25

    八、匿名函数

    没有函数名称的函数。

    可赋值给变量,通过变量调用,可以把匿名函数看成一个对象。也可在其他函数中直接调用或传递给其他函数。

    1. var printNoParamsFun = () => print("Hello"); //无参匿名函数
    2. var printParamsFun = (param) => print(param); //有参匿名函数
    3. (()=>print("hello"))(); //不推荐
    4. int Function(int i) anonymousFuc = (int i) => i + 1;
    5. print(anonymousFuc(1)+3); //5
    6. //源码中的forEach方法
    7. void forEach(void f(E element)) {
    8.    for (E element in this) f(element);
    9. }

    九、支持typedef(函数别名)

    typedef就是给定义的函数取个别名,然后就可以使用operationFun来声明一个函数变量,这样就能清晰的表明了函数参数的类型及返回值的类型。

    1. void main(){
    2.  OperationFun add(int a, int b){
    3.    print(a+b);
    4. }
    5.  OperationFun fun = add(1, 2);
    6. }
    7. typedef OperationFun(int a, int b);

    十、类函数

    所谓类函数类似于java中的静态方法,就是在函数前面加个static。

    1. class Clazz {
    2.  static Object func() {
    3.    return null;
    4. }
    5. }

  • 相关阅读:
    ES7+向量检索实现方法
    2022-08-03
    企业微信把人移出会有显示吗?如何移出?
    flink-cep实践
    【自动驾驶地图】OpenDrive协议总结(上)
    全志T507 UART复用方法-飞凌嵌入式知识库
    【Python】新手入门学习:详细介绍里氏替换原则(LSP)及其作用、代码示例
    从零开始的LINUX(一)
    上传图片和视频在JAVA上的运用
    SpringBoot基于Netty实现对接硬件,接受硬件报文
  • 原文地址:https://blog.csdn.net/life491249957/article/details/126023239