Dart(5)-内置类型

一、Numbers数值

在dart中数字类型只有int和double两种,int和double的最大长度是64位,范围在2的63次方到负2的63次方减1,num是int和double的父类,如下代码:

part of dart.core;
abstract class int extends num {...}
abstract class double extends num {...}
abstract class num implements Comparable<num> {
  bool operator ==(Object other);
  int get hashCode;
  int compareTo(num other);
  num operator +(num other);
  num operator -(num other);
  num operator *(num other);
  num operator %(num other);
  double operator /(num other);
  int operator ~/(num other);
  num operator -();
  num remainder(num other);
  bool operator <(num other);
  bool operator <=(num other);
  bool operator >(num other);
  bool operator >=(num other);
  bool get isNaN;
  bool get isNegative;
  bool get isInfinite;
  bool get isFinite;
  num abs(); //绝对值
  num get sign;
  int round();//经过四舍五入得到整数
  int floor();//求向下的最大整数
  int ceil();//求向上最大的整数 
  int truncate();//截取掉小数点取整
  ...
}

二、String

(1)Dart 字符串是 用UTF-16 编码的字符序列,可以使用单引号双引号来创建字符串。

(2)可以使用三个单引号或者双引号创建多行字符串对象。

(3)使用+或者空格进行拼接字符串、也可以在字符串中使用转义字符

(4)可以使用 r 前缀创建”原始raw”字符串。

(5)可以在字符串中使用表达式: ${expression},如果表达式是一个标识符,可以省略 {},如果表达式的结果为一个对象,则 Dart 会调用对象的 toString() 函数来获取一个字符串。

String str1 = 'helloworld'; //单引号定义
String str2 = "helloworld" + 'abc'; //双引号定义 与 字符串相加
String str3 = '''
    helloworld1
    helloworld2
    helloworld3
    '''; // 多行字符串
String str4 = '''
    helloworld1\nhelloworld2
    helloworld3
    '''; // \n换行,\转义特殊字符
String str5 = r'''
    helloworld1\nhelloworld2
    helloworld3'''; //字符串前加r(raw),打印原始数据
String str6 = "abc" "def";
String str7 = "输出str6的值$str6"; //使用$符号输出变量的值

三、Booleans

布尔,编程中必须有的,dart为我们提供布尔关键字是bool。但是有点不同,java中boolean默认是false,而dart中的bool默认值是null。如下:

bool isTrue;
 print(isTrue ? "true" : 'false');
Failed assertion: boolean expression must not be null
#0      main (package:flutter_demo/dartEx.dart:75:9)
#1      _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:301:19)
#2      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)

另外:if表达式中必须是bool类型,不然编译器会提示错误。

四、Object类型

在Dart语言中所有东西都是对象,都继承于Object, 所以可以使用Object可以定义任何的变量,而且赋值后,类型也可以更改。

五、Lists集合

(1)Dart中可以直接打印List内的元素,而不是像Java打印List对象的地址。

(2)List集合的下标是从0开始

(3)支持泛型

六、Sets

set1.difference(set2):返回set1集合里有但set2里没有的元素集合

set1.intersection(set2):返回set1和set2的交集

set1.union(set2):返回set1和set2的并集

set1.retainAll():set1只保留某些元素(要保留的元素要在原set中存在)

七、Maps

Map集合是以键值对的形式,键和值中可以是任何类型(包括null)。在一个Map对象中键不能重复,而值可以重复,或者说每个建都有一个与之关联的值。Map的实现类有HashMap(无序)、LinkedHashMap(按键插入序列)、SplayTreeMap(按键的排序顺序)。通常不允许在正在Map上执行操作,例如在调用在[forEach]或[putIfAbsent]调用期间,在迭代键或值时修改映射也可能打断迭代。

Map的创建

(1) 花括号创建

var map = {"key1":"value1", "key2":"value2", ..., "keyN":"valueN"};//不指定泛型
var map2 = <String,String>{"key1":"value1", "key2":"value2", ..., "keyN":"valueN"};//指定泛型

(2) 构造器创建

Map map = new HashMap();//不指定泛型
Map<String,String> map2 = new HashMap();//指定泛型
map2["key1"] = "value1"; //赋值

八、Runes符号字符串

Dart字符串是UTF-16代码单元序列,而Runes 对象是一个UTF-32字节单元定义的Unicode字符串,它可以通过文字转换成符号表情或者代表特定的文字。这样设计也是考虑兼容 UTF-16 四个字节的情况。dart:core库中的String类提供了访问符文的方法,如下:

  • 使用String.codeUnitAt()函数

  • 使用String.codeUnits属性

  • 使用String.runes属性

九、Symbols标识符

Symbol对象表示在Dart程序中声明的运算符或标识符。您可能永远不需要使用符号,但它们对于按名称引用标识符的API非常有用,因为缩小会更改标识符名称而不会更改标识符符号。要获取标识符的符号,请使用符号文字,它只是#后跟标识符。一般Symbols用于Dart中的反射,但是注意在Flutter中禁止使用反射。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值