flutter【2】dart语言--内置数据类型

内置类型

  • 数值型
  • 字符串
  • 布尔型
  • 列表/数组
  • Map
  • runes(表示字符串中的 unicode 字符)
  • symbols

上述类型的变量多可以用字面量进行初始化,例如 'this is a string' 是一个字符串字面量,true 是 boolean 字面量

dart中变量都是引用的对象,所以可以用构造器进行初始化,部分内置变量有自己的构造器,例如 Map()。

数值型

dart的数值型有两种:

  • int:整型,位宽由运行平台决定,但是不大于 64 bit
  • double:IEEE754 标准定义的 64 bit 双精度浮点数

int 和 double 都是 num 的子类型,包括加减乘除(+ - * /)操作,abs(),ceil(),floor(),等方法。(位移操作 >> 在 int 中定义)。如果基础操作满足不了需求,可以使用 dart:math 库。

在dart2.1之后,整型字面量可以自动转换为 double 型: double z = 1; //等价于 double z = 1.0

字符串与整型互相转换

// String -> int
var one = int.parse('1');
assert(one == 1);

// String -> double
var onePointOne = double.parse('1.1');
assert(onePointOne == 1.1);

// int -> String
String oneAsString = 1.toString();
assert(oneAsString == '1');

// double -> String
String piAsString = 3.14159.toStringAsFixed(2);
assert(piAsString == '3.14');

整型的位运算、与运算、或运算

assert((3 << 1) == 6); // 0011 << 1 == 0110
assert((3 >> 1) == 1); // 0011 >> 1 == 0001
assert((3 | 4) == 7); // 0011 | 0100 == 0111

数值型字面量是编译时常量。由编译时常量组成的算术表达式也是编译时常量。

const msPerSecond = 1000;
const secondsUntilRetry = 5;
const msUntilRetry = secondsUntilRetry * msPerSecond;

String

dart 使用的是 utf-16 的字符集,可以用单引号或者双引号创建。

var s1 = 'Single quotes work well for string literals.';
var s2 = "Double quotes work just as well.";
var s3 = 'It\'s easy to escape the string delimiter.';
var s4 = "It's even easier to use the other delimiter.";

使用 ${expression} 可以在字符串中使用表达式的值,如果表达式就是一个标识符,可以省略大括号。

    var s = 'string interpolation';

    assert('Dart has $s, which is very handy.' ==
        'Dart has string interpolation, ' + 'which is very handy.');
    assert('That deserves all caps. ' + '${s.toUpperCase()} is very handy!' ==
        'That deserves all caps. ' + 'STRING INTERPOLATION is very handy!');

dart 中 == 用来判断连个对象是否相等,字符串只要字符内容相同就表示相等。

连接字符串可以用 + 操作符,也可以用相邻的字符串字面量。

    var s1 = 'String '
        'concatenation'
        " works even over line breaks.";
    assert(s1 ==
        'String concatenation works even over '
        'line breaks.');

    var s2 = 'The + operator ' + 'works, as well.';
    assert(s2 == 'The + operator works, as well.');

创建多行字符串:使用三个单引号或者双引号。

    var s1 = '''
You can create
multi-line strings like this one.
''';

    var s2 = """This is also a
multi-line string.""";

raw字符串:其中的转义符不会被处理

var s = r'In a raw string, not even \n gets special treatment.';

更多信息参考Strings and regular expressions

布尔型

dart中用 bool 类型来表述布尔数据,只有两个对象有 bool 类型:true 和 false(boolean字面量,编译时常量)

dart的类型安全意思是不可用如下代码

if(nonbooleanValue) ……

assert(nonbooleanValue)

列表

dart中 List 对象表示数组,也就是列表。

字面量列表:var list = [1, 2, 3]; , 会自动推断为 List<int> 列表,如果后面插入非 int 型对象,将会出错,具体信息请参考type inference

列表索引从 0 开始,到 lsit.length - 1 结束。

创建编译时常量列表需要在字面量列表前加上 const 关键字。 var constList = const [1, 2, 3];

更多信息请参考GenericsCollections

Maps

key-value结构,dart 中用 Map类型表示。

    var gifts = {
      // Key:    Value
      'first': 'partridge',
      'second': 'turtledoves',
      'fifth': 'golden rings'
    };

    var nobleGases = {
      2: 'helium',
      10: 'neon',
      18: 'argon',
    };

gifts 推断为 Map<String, String>;nobleGases 推断为 Map<int, String>。

可以使用 Map 的构造器创建Map对象,其中 new 关键字可以加也可以省略。

    var gifts = Map();
    gifts['first'] = 'partridge';
    gifts['second'] = 'turtledoves';
    gifts['fifth'] = 'golden rings';

    var nobleGases = Map();
    nobleGases[2] = 'helium';
    nobleGases[10] = 'neon';
    nobleGases[18] = 'argon';

更多信息请参考GenericsMaps

Runes

runes是dart中表示utf-32格式字符的编码单元。(In Dart, runes are the UTF-32 code points of a string.)

unicode 为全世界在用的所有字母、数字、符号都定义了一个独一无二的数值,但是dart的字符序列使用的是 utf-16的编码格式,所以表示utf-32unicode格式的值需要特殊的语法。

通常情况下一个unicode编码格式为 \uXXXX,XXXX是4位16进制的数值,如 \u2665,当要表示多于或者少于4位的时,用大括号包起来,如 \u{1f600}。

String 类提供了获取 rune 信息的属性,比如 codeUnitAt 和 codeUnit 获取 字节的16-bit 编码;使用runes属性获取字符串的runes。

可以用如下代码展示runes、16位编码单元和32位编码单元的关系。

main() {
  var clapping = '\u{1f44f}';
  print(clapping);
  print(clapping.codeUnits);
  print(clapping.runes.toList());

  Runes input = new Runes(
      '\u2665  \u{1f605}  \u{1f60e}  \u{1f47b}  \u{1f596}  \u{1f44d}');
  print(new String.fromCharCodes(input));
}

需要注意的是操作 runes 列表时很容易因为特殊的语言、字符和操作而崩溃,具体参考How do I reverse a String in Dart?

Symbols

参考官网描述吧

猜你喜欢

转载自blog.csdn.net/weixin_34415923/article/details/87201973