Dart 語言簡易教程(一): http://www.lxweimin.com/p/8a62b1a2fd75
Dart 語言簡易教程(二): http://www.lxweimin.com/p/b2153a32dd8b
Dart 語言簡易教程(三): http://www.lxweimin.com/p/6d2495a0d3d7
Dart 語言簡易教程(四): http://www.lxweimin.com/p/fdd046a6dc82
Dart 語言簡易教程(五) http://www.lxweimin.com/p/83adc77839b6
Dart 語言簡易教程(六) http://www.lxweimin.com/p/78d317b2ea79
Dart 語言簡易教程(七) http://www.lxweimin.com/p/023cea0b4a52
Dart 語言簡易教程(八)
可調(diào)用的類(Callable classes)
Dart 語言中為了能夠讓類像函數(shù)一樣能夠被調(diào)用,可以實(shí)現(xiàn)call()
方法。
實(shí)例代碼:
class WannabeFunction {
call(String a, String b, String c) => '$a $b $c!';
}
main() {
var wf = new WannabeFunction();
var out = wf("Hi","there,","gang");
print('$out');
}
上面代碼的輸出結(jié)果為:
Hi there, gang!
隔離
Dart 語言中每個(gè)app都運(yùn)行在獨(dú)立的隔離空間中,每個(gè)隔離空間都有自己的內(nèi)存堆棧,確保每個(gè)隔離空間的狀態(tài)不會(huì)被其它的隔離空間訪問。
隔離空間的概念和類似與Chrome 瀏覽器的沙盒概念,每個(gè)應(yīng)用獨(dú)自運(yùn)行在自己的空間里,互不干擾。
這樣做的好處是:安全,減少了代碼復(fù)雜性。
類型定義(Typedefs)
在Dart 語言中,函數(shù)也是一個(gè)對(duì)象,和字符串或數(shù)字一樣。
原始代碼:
class SortedCollection {
Function compare;
SortedCollection(int f(Object a, Object b)) {
compare = f;
}
}
// Initial, broken implementation.
int sort(Object a, Object b) => 0;
main() {
SortedCollection coll = new SortedCollection(sort);
// All we know is that compare is a function,
// but what type of function?
assert(coll.compare is Function);
}
使用了typedef 的代碼:
typedef int Compare(Object a, Object b);
class SortedCollection {
Compare compare;
SortedCollection(this.compare);
}
// Initial, broken implementation.
int sort(Object a, Object b) => 0;
main() {
SortedCollection coll = new SortedCollection(sort);
assert(coll.compare is Function);
assert(coll.compare is Compare);
}
因?yàn)?code>typedef只是簡單的別名,通過用來檢查函數(shù)類型。
例子如下:
typedef int Compare(int a, int b);
int sort(int a, int b) => a - b;
main() {
assert(sort is Compare); // True!
}
元數(shù)據(jù)(Metadata)
使用元數(shù)據(jù)給代碼添加更多的信息。
元數(shù)據(jù)是以@
開始的修飾符。在@
后面接著編譯時(shí)的常量或調(diào)用一個(gè)常量構(gòu)造函數(shù)。
目前Dart語言提供的三個(gè)可用的@修飾符:
- @deprecated
- @override
- @proxy
使用@deprecated
修飾符的例子:
class Television {
/// _Deprecated: Use [turnOn] instead._
@deprecated
void activate() {
turnOn();
}
/// Turns the TV's power on.
void turnOn() {
print('on!');
}
}
元數(shù)據(jù)可以修飾library(庫), class(類), typedef(類型定義), type parameter(類型參數(shù)), constructor(構(gòu)造函數(shù)), factory(工廠函數(shù)), function(函數(shù)), field(作用域), parameter(參數(shù)), 或者 variable declaration(變量聲明)。
定義自己的元數(shù)據(jù)
通過library
來定義一個(gè)庫,在庫中定義一個(gè)相同名字的類,然后在類中定義const 修飾的相同名字的方法。
元數(shù)據(jù)定義的方法:
library todo;
class todo {
final String who;
final String what;
const todo(this.who, this.what);
}
元數(shù)據(jù)使用的例子:
import 'todo.dart';
@todo('seth', 'make this do something')
void doSomething() {
print('do something');
}
注釋
Dart 支持三種注釋類型:
- 單行注釋
- 多行注釋
- 文檔注釋
單行注釋
單行注釋以//
開頭,從//
開始到一行結(jié)束的所以內(nèi)容都會(huì)被Dart 編譯器忽略。
main() {
// TODO: refactor into an AbstractLlamaGreetingFactory?
print('Welcome to my Llama farm!');
}
多行注釋
單行注釋以/*
開頭,以*/
結(jié)束。從/*
開頭到*/
結(jié)束間的所有內(nèi)容都會(huì)被Dart 編譯器忽略掉。
main() {
/*
* This is a lot of work. Consider raising chickens.
Llama larry = new Llama();
larry.feed();
larry.exercise();
larry.clean();
*/
}
文檔注釋
文檔注釋以/**
或///
開頭
/// A domesticated South American camelid (Lama glama).
///
/// Andean cultures have used llamas as meat and pack
/// animals since pre-Hispanic times.
class Llama {
String name;
/**
* Feeds your llama [Food].
* The typical llama eats one bale of hay per week.
**/
void feed(Food food) {
// ...
}
/// Exercises your llama with an [activity] for
/// [timeLimit] minutes.
void exercise(Activity activity, int timeLimit) {
// ...
}
}