Dart的資料格式
可在此網址測試
https://dartpad.dev/?null_safety=true
void main() {
var number = 1;
var bool = number is int; //String //double
print(bool); // true
dynamic numberOrString = 1;
numberOrString = '1';
//--------------------------------------------//
List<dynamic> list = [1, '1', true];
print(list);
list.forEach((item) => print(item)); //參數括弧不可拿掉(不同於js)
for(var item in list) print(item);
list.add('a');
print(list); //[1, 1, true, a]
List<dynamic> list2 = [2];
list2.addAll(list);
print(list2); //[2, 1, 1, true, a]
list2.clear();
print(list2); // []
print(list.indexOf('a')); //3
print(list.map((e) => 5).toList()); //[5, 5, 5, 5] //toList是為了讓iterable變成list
//--------------------------------------------//
var map = {
'key': 'value',
'key2': 'value2'
};
print(map['key']); //value
print(map['willBeNull']); //null
print(map.length); //2
print(map.containsKey('key')); //true
print(map.entries); //(MapEntry(key: value), MapEntry(key2: value2))
print(map.values); //(value, value2)
print(map.keys); //(key, key2)
print(map.values.toList()); //[value, value2]
print(map.keys.toList()); //[key, key2]
Map<String, String> map2 = {
"a": "2"
};
map2.addAll(map);
print(map2); //{a: 2, key: value, key2: value2}
print(map2.containsValue('value')); //ture
Map<String, String> map3 = map2.map((key,value) => MapEntry(key, '${value}5566'));
print(map3); // {a: 25566, key: value5566, key2: value25566}
map3.putIfAbsent('b', () => '7788');
print(map3); //{a: 25566, key: value5566, key2: value25566, b: 7788}
map3.removeWhere((key, value) => key == 'b');
print(map3); //{a: 25566, key: value5566, key2: value25566}
//--------------------------------------------//
String fun(int number) {
return "${number}";
}
print(fun(number)); //1
String fun2(int number) => "${number}";
print(fun2(number)); //1
String fun3({ int number=3 }) => "${number}";
print(fun3()); //3 (括弧內參數可有可無)
print(fun3(number: 4)); //4 (若要傳入就要指定)
try {
throw new IntegerDivisionByZeroException();
}catch(e) {
print(e);
}finally {
print('wil be here whether success or fail');
}
//--------------------------------------------//
var person = Person(h: 177, n: "Jerry");
print(person.height); //177
var person2 = Person.defaultPerson();
print(person2.height); //100
person2.setHeight(200);
print(person2.getHeight()); //200
var badPerson = BadPerson();
print(badPerson.height); //0
var goodPerson = GoodPerson(h2: 999, n2: "Sony");
print(goodPerson.height); //999
print(identical(badPerson, goodPerson)); //false
}
class Person {
int height = 0;
String name = "someone";
final country = "taiwan";
Person({ int h: 0, String n: 'someone' } ) {
assert(h == 177, 'not good'); //若在手機跑就會停在這裡
this.height = h;
this.name = n;
}
//等同於以下code
//Person(this.height, this.name) : assert(height ==177, 'not good');
//若要接下一個assert就用逗號隔開
Person.defaultPerson(){} //不用分號
int getHeight() => this.height;
void setHeight(int h) => this.height = h;
// void setCountry(String c) => this.country = c;(不能這樣寫,因為是final)
}
class BadPerson extends Person {
}
class GoodPerson extends Person {
GoodPerson({ int h2: 180, String n2: 'GoodMan' }) : super(h: h2, n: n2);
//super等於Person //同Person(h: 177, n: "Jerry");
//小孩的傳參放入爸爸的傳參
}
沒有留言:
張貼留言