99re热这里只有精品视频,7777色鬼xxxx欧美色妇,国产成人精品一区二三区在线观看,内射爽无广熟女亚洲,精品人妻av一区二区三区

內(nèi)建類型(上)

2020-02-03 23:57 更新

Dart 語(yǔ)言支持以下內(nèi)建類型:

  • Number
  • String
  • Boolean
  • List (也被稱為 Array)
  • Map
  • Set
  • Rune (用于在字符串中表示 Unicode 字符)
  • Symbol

這些類型都可以被初始化為字面量。 例如, 'this is a string' 是一個(gè)字符串的字面量, true 是一個(gè)布爾的字面量。

因?yàn)樵?Dart 所有的變量終究是一個(gè)對(duì)象(一個(gè)類的實(shí)例), 所以變量可以使用 構(gòu)造涵數(shù) 進(jìn)行初始化。 一些內(nèi)建類型擁有自己的構(gòu)造函數(shù)。 例如, 通過(guò) Map() 來(lái)構(gòu)造一個(gè) map 變量。


Number

Dart 語(yǔ)言的 Number 有兩種類型:

int

整數(shù)值不大于64位, 具體取決于平臺(tái)。 在 Dart VM 上, 值的范圍從 -263 到 263 - 1. Dart 被編譯為 JavaScript 時(shí),使用 JavaScript numbers, 值的范圍從 -253 到 253 - 1.

double

64位(雙精度)浮點(diǎn)數(shù),依據(jù) IEEE 754 標(biāo)準(zhǔn)。

int 和 double 都是 num. 的亞類型。 num 類型包括基本運(yùn)算 +, -, /, 和 *, 以及 abs(), ceil(), 和 floor(), 等函數(shù)方法。 (按位運(yùn)算符,例如?,定義在 int 類中。) 如果 num 及其亞類型找不到你想要的方法, 嘗試查找使用 dart:math 庫(kù)。

整數(shù)類型不包含小數(shù)點(diǎn)。 下面是定義整數(shù)類型字面量的例子:

var x = 1;
var hex = 0xDEADBEEF;

如果一個(gè)數(shù)字包含小數(shù)點(diǎn),那么就是小數(shù)類型。 下面是定義小數(shù)類型字面量的例子:

var y = 1.1;
var exponents = 1.42e5;

從 Dart 2.1 開(kāi)始,必要的時(shí)候 int 字面量會(huì)自動(dòng)轉(zhuǎn)換成 double 類型。

double z = 1; // 相當(dāng)于 double z = 1.0.

版本提示: 在 2.1 之前,在 double 上下文中使用 int 字面量是錯(cuò)誤的。

以下是將字符串轉(zhuǎn)換為數(shù)字的方法,反之亦然:

// 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');

int 特有的傳統(tǒng)按位運(yùn)算操作,移位(<<, >>),按位與(&)以及 按位或(|)。 例如:

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

數(shù)字類型字面量是編譯時(shí)常量。 在算術(shù)表達(dá)式中,只要參與計(jì)算的因子是編譯時(shí)常量, 那么算術(shù)表達(dá)式的結(jié)果也是編譯時(shí)常量。

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


String

Dart 字符串是一組 UTF-16 單元序列。 字符串通過(guò)單引號(hào)或者雙引號(hào)創(chuàng)建。

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.";

字符串可以通過(guò) ${expression} 的方式內(nèi)嵌表達(dá)式。 如果表達(dá)式是一個(gè)標(biāo)識(shí)符,則 {} 可以省略。 在 Dart 中通過(guò)調(diào)用就對(duì)象的 toString() 方法來(lái)得到對(duì)象相應(yīng)的字符串。

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!');

提示: == 運(yùn)算符用來(lái)測(cè)試兩個(gè)對(duì)象是否相等。 在字符串中,如果兩個(gè)字符串包含了相同的編碼序列,那么這兩個(gè)字符串相等。 units.

可以使用 + 運(yùn)算符來(lái)把多個(gè)字符串連接為一個(gè),也可以把多個(gè)字面量字符串寫在一起來(lái)實(shí)現(xiàn)字符串連接:

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.');

使用連續(xù)三個(gè)單引號(hào)或者三個(gè)雙引號(hào)實(shí)現(xiàn)多行字符串對(duì)象的創(chuàng)建:

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

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

使用 r 前綴,可以創(chuàng)建 “原始 raw” 字符串:

var s = r"In a raw string, even \n isn't special.";

參考 Runes 來(lái)了解如何在字符串中表達(dá) Unicode 字符。

一個(gè)編譯時(shí)常量的字面量字符串中,如果存在插值表達(dá)式,表達(dá)式內(nèi)容也是編譯時(shí)常量, 那么該字符串依舊是編譯時(shí)常量。 插入的常量值類型可以是 null,數(shù)值,字符串或布爾值。

// const 類型數(shù)據(jù)
const aConstNum = 0;
const aConstBool = true;
const aConstString = 'a constant string';

// 非 const 類型數(shù)據(jù)
var aNum = 0;
var aBool = true;
var aString = 'a string';
const aConstList = [1, 2, 3];

const validConstString = '$aConstNum $aConstBool $aConstString'; //const 類型數(shù)據(jù)
// const invalidConstString = '$aNum $aBool $aString $aConstList'; //非 const 類型數(shù)據(jù)

更多關(guān)于 string 的使用, 參考 字符串和正則表達(dá)式。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)