Functions (Methods) of Dart details that you don't know

Preface

The first two articles have explained the variables and built-in types in Dart respectively, and have known the basic syntax of Dart. Today, let's talk about the functions in Dart, that is, methods. If you haven't read the first two articles, it's suggested to read the first two articles first.

Definition

  • Can be defined within a function

    void main(){
      void test(){
      }
    }
    

    Unlike Java, functions in Dart can be defined inside functions (confused with Java's anonymous inner categories)

  • Type can be omitted when defining function (not recommended)

    main(){
      test(){
        // return null;
      }
    }
    

    The above Function can be written as follows: in Dart, the Function is an object of Function type. All functions return a value. If no return value is specified, the statement return null is executed as the last statement of the Function by default.

  • Support abbreviation syntax = > (Kotlin is implemented with =)

    main(){
      print(test1());
    }
    test()=> "zhujiang";
    String test1(){
      return "zhujiang";
    }
    

    The effect of test() and test1() in the above code is the same, "= >" is equivalent to braces and return

Optional parameters

Since it's a function, I have to pass parameters. After being destroyed by Java construction methods, when I look at this function, I burst into tears It is. Why is that? Let's take a look at the constructor of our Java class:

public class Test {

    private String name;
    private int age;
    private String address;

    public Test(String name, int age, String address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }
    
    public Test(String name, String address) {
        this.name = name;
        this.address = address;
    }

    public Test(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Test(String name) {
        this.name = name;
    }
}

A very simple construction method, just want to pass parameter form a little more, unexpectedly need to write so many overloaded methods... Let's take a look at the code with the same function written by Dart:

class Test{
  String name;
  int age;
  String address;
  Test(this.name, {this.age, this.address});
}

What? How could that be? Yes, it can be. Let's talk about the optional parameters of the function in detail.

Optional named parameters

In fact, the above construction method I use is the optional named parameter, which is very simple when I write, just need to add braces. Here is how to use it:

main() {
  print(add(a: 1, b: 3));
}

int add({int a = 1, int b}) {
  return a + b;
}

When the above add method is called, you can either fill in any parameter or fill in all parameters. Note here:

  • If a is not filled in, the default value is 1. If b is not filled in, the default value is null.
  • When passing parameters, remember to write a: otherwise, an error will be reported.

Optional location parameters

Different from the optional named parameter above, if the function above wants to be called, it must be named plus a colon to use it, and the optional location parameter is not needed. As above, the default value can be added or not:

int add2(int a,[int b = 2,int c = 3]) {
  return a + b;
}

Here are several ways to call the add2() method above:

  print(add2(1));
  print(add2(1,2));
  print(add2(1,1,3));

Be careful

The above optional named parameter and optional location parameter can use list or map as the default value, but must be const.

int add3({List a = const [1,2,3], int b = 2}) {
  return b;
}

Anonymous function

It can be assigned to variable and called through variable. It can be called directly in other functions or passed to other functions. Anonymous functions are divided into parametrically anonymous functions and parametrically anonymous functions

  • Nonparametric anonymous function

    var printFun = () => print("**Nonparametric anonymous function**");
    
  • Parametric anonymous function

    var printFun2 = (name) => print("**Parametric anonymous function $name**");
    printFun2("sss");
    

There is also a little knowledge here: it can be called through (), which is not recommended.

(() =>print("***Can pass()Call, do not push"))();

closure

There's nothing to say here. You can understand it by looking at the code directly:

Function makeAddFunc(int x) {
  x++;
  return (int y) => x + y;
}
var makeAddFun = makeAddFunc(11);
print(makeAddFun(10));

It is to create a method and return a method, which can be called again.

Function alias

It is convenient to use typedef to give an alias to a function. For example, to define a callback of a method, use the alias definition directly. No return value, as long as the parameter matches, if the return value is defined, an error will be reported if the return value is different.

typedef Fun1(int a, int b);
typedef Fun2<T, K>(T a, K b);
int add(int a, int b) {
  print('a + b');
  return a + b;
}
class Demo1 {
  Demo1(int f(int a, int b), int x, int y) {
    var sum = f(x, y);
    print("sum1 = $sum");
  }
}
class Demo2 {
  Demo2(Fun1 f, int x, int y) {
    var sum = f(x, y);
    print("sum2 = $sum");
  }
}
class Demo3 {
  Demo3(Fun2<int, int> f, int x, int y) {
    var sum = f(x, y);
    print("sum3 = $sum");
  }
}

The above code is the method to define the function alias. The following is the calling method:

Fun1 fun1 = add(11, 12);

summary

This is the end of Dart's third article. This article focuses on the functions (Methods) of Dart language. The next article will explain the operators, process control statements and exceptions in Dart. I hope you can like them. If you are helpful, please like them. If you have any objections, you can leave a message for discussion.

Published 94 original articles, won praise 255, and visited 120000+
Private letter follow

Tags: Java

Posted on Sun, 08 Mar 2020 07:28:17 -0400 by djjjozsi