了解Dart中的括号(颤振) [英] Understanding Brackets in Dart (Flutter)

查看:38
本文介绍了了解Dart中的括号(颤振)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Dart (Flutter)中的括号感到困惑.哪个括号(),{},[]"用于什么?

I have many confusion about Brackets in Dart(Flutter). Which bracket "(), {}, []" is used for what?

推荐答案

  • ()可以对表达式进行分组:

    • () can group expressions:

      var x = (1 + 2) * 3;
      

      或可以指定函数的参数列表:

      or can designate parameter lists for functions:

      var getAnswer = () => 42;
      
      int square(int x) => x * x;
      

      或可以指定函数调用:

      var answer = getAnswer();
      var squared = square(4);
      

      or是某些关键字语法的一部分.这包括(但不限于) if assert for while switch catch :

      or is part of the syntax to some keywords. This includes (but is not limited to) if, assert, for, while, switch, catch:

      if (condition) {
        ...
      }
      
      assert(condition);
      
      for (var item in collection) {
        ...
      }
      
      
      while (condition) {
        ...
      }
      
      switch (value) {
        ...
      }
      
      try {
        ...
      } on Exception catch (e) {
        ...
      }
      

    • [] 本身会创建 列出文字:

    • [] by itself creates List literals:

      var list = [1, 2, 3];
      var emptyList = []; // Creates a List<dynamic>.
      

      或在对象上使用时,调用 operator [] ,通常访问集合的元素:

      or when used on an object, calls operator [], which usually accesses an element of a collection:

      var element = list[index];
      var value = map[key];
      

      或在参数列表中,指定可选的 positional 参数:

      or in a parameter list, specifies optional positional parameters:

      int function(int x, [int y, int z]) {
        return x + y ?? 0 + z ?? 0;
      }
      
      function(1, 2);
      

      在dartdoc注释中

      ,创建对其他符号的链接引用:

      or in dartdoc comments, creates linked references to other symbols:

      /// Creates a [Bar] from a [Foo].
      Bar fooToBar(Foo foo) {
        // ...
      }
      

    • {} 可以创建代码块,将行分组在一起并限制变量范围.这包括(但不限于)函数体,类声明, if - else 块, try - catch 块, for 块, while 块, switch 块等:

    • {} can create a block of code, grouping lines together and limiting variable scope. This includes (but is not limited to) function bodies, class declarations, if-else blocks, try-catch blocks, for blocks, while blocks, switch blocks, etc.:

      class Class {
        int member;
      }
      
      void doNothing() {}
      
      void function(bool condition) {
        {
          int x = 'Hello world!';
          print(x);
        }
      
        if (condition) {
          int x = 42; // No name collision due to separate scopes.
          print(x);
        }
      }
      

      或单独创建 Set Map 文字:

      or by itself can create Set or Map literals:

      var set = {1, 2, 3};
      var emptySet = <int>{};
      
      var map = {'one': 1, 'two': 2, 'three': 3};
      var emptyMap = {}; // Creates a Map<dynamic, dynamic>
      

      或在参数列表中,指定可选的命名参数:

      or in a parameter list, specifies optional named parameters:

      int function(int x, {int y, int z}) {
        return x + y ?? 0 + z ?? 0;
      }
      
      function(1, y: 2);
      

      或创建枚举:

      enum SomeEnumeration {
        foo,
        bar,
        baz,
      }
      

      String 中的

      用于消除插值表达式的歧义:

      or in Strings is used to disambiguate interpolated expressions:

      var foo = 'foo';
      var foobar = '${foo}bar';
      
      var s = '${function(1, 2)}';
      

    • <>> 在函数或类声明中成对使用时会创建

    • <> when used as a pair in function or class declarations creates generics:

      class GenericClass<T> {
        T member;
      }
      
      T function<T>(T x) {
        // ...
      }
      

      使用泛型时指定显式类型:

      or specifies explicit types when using generics:

      var map = <String, int>{};
      

    • 这篇关于了解Dart中的括号(颤振)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆