15. Class
Swift
class Sample {
var x:Int
init (x: Int) {
self.x = x
}
func someMethod() {
self.hi()
}
}
var sample = Sample(x: 1)
Dart
class Sample {
int x = 0;
Sample(this.x) {
}
someMethod() {
this.hi();
}
}
Sample sample = new
Sample(1);
17. 匿名函式
Swift
{ x in …
}
或
{ $0.doSomething() }
Dart
(int x) {
…
}
或寫成單⾏行行
(x) => doSomething(x);
18. 匿名函式
Swift
class MyClass {
var f: ((Int)->Int)?
}
var x = MyClass()
x.f = { $0 + 1 }
print("(x.f!(2))"
// Swift 也可以⽤用 typealias
Dart
// ⼀一定要⽤用 typedef
typedef int Call(int);
class MyClass {
Call f;
}
main() {
var x = new MyClass();
x.f = (i) => i + 1;
print(x.f(2));
}