Dart 内置类型.md
title: Dart 内置类型date: 2021-07-15 11:01:08.916
updated: 2021-07-15 12:40:46.16
url: /?p=297
categories: Dart
tags:
内置的类型
与Java的八大内置基本数据类型不同,Dart的类型都是类,Dart 内置支持下面这些类型:
- numbers
- strings
- booleans
- lists (也被称之为 arrays)
- maps
- runes (用于在字符串中表示 Unicode 字符)
- symbols
Numbers(数值)
num
是数字类型的父类,有两个子类int
和 double
。
int
1 | part of dart.core; |
- int类型继承自num类型。
- int类型表示一个整型。
- int类型占用多少个字节应该看运行环境而定。但是最大还是8个字节。
- Dart中的int可以当成Java中的short&int&long,如果数字比较小,相当于使用java中的short&int,超过4个字节,那么就确定为long类型,我个人认为它是动态确定的。
double
1 |
|
Strings(字符串)
Dart 字符串是 UTF-16 编码的字符序列。 可以使用单引号或者双引号来创建字符串,单引号和双引号可以嵌套使用,否则需要使用\
进行转义。字符串中也可以引用变量与表达式。
1 | var name = 'lautung'; |
1 | // 单双引号声明 |
与Java一样可以使用 +
操作符来把拼接字符串,也可以把多个 字符串放到一起来实现同样的功能:
1 | var a = "my name is " "lance"; |
使用三个单引号或者双引号可以 创建多行字符串对象
1 | var s1 = ''' |
提供一个 `r` 前缀可以创建一个 “原始 raw” 字符串
1 | print(r"换行符:\n"); // 换行符:\n r:不需要转义 |
Booleans(布尔值)
Dart 有一个名字为 bool
的类型。 只有两个对象是布尔类型的:true
和 false
。这一点和Java没有太大的区别。
Lists(列表&数组)
几乎所有编程语言中最常见的集合可能是数组或有序对象组。在Dart中,数组就是List
对象。对List
进行遍历也和Java一样。
1 | var list = [1, 2, 3]; |
遍历数组:
1 | // 遍历数组 |
const list:
1 | // list对象不可变 |
1 | //const 修饰的是变量,也就是引用,那么变量就不能够再重新引用其他的对象了, |
Maps(映射集合)
Map:键值对相关的对象。 键和值可以是任何类型的对象。每个 键 只出现一次, 而一个值则可以出现多次。
1 | //直接声明,用{}表示,里面写key和value,每组键值对中间用逗号隔开 |
与List一样,在 map字面量之前添加 const
关键字,可以 定义一个 编译时常量 的 map
Runes(用于在字符串中表示Unicode字符)
如果需要获得特殊字符的Unicode编码,或者需要将32位的Unicode编码转换为字符串,就可以借助Runes类。
Dart表达Unicode代码点的常用方法是\uXXXX,其中XXXX是4位十六进制值。要指定多于或少于4个十六进制数字,需要将值放在大括号中。
1 | var clapping = '\u{1f44f}'; ///5个16进制 需要使用{} |
实际上在Flutter开发中Runes与下一个Symbols可能永远也不会用到。
Symbols
操作符标识符,可以看作C中的宏。表示编译时的一个常量
1 | var i = #A; //常量 |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 LT的编程笔记!