Android开发Android开发经验谈Android技术知识

Dart与Java不同的地方

2019-10-10  本文已影响0人  喂_balabala

数据类型

运算符: +、 - 、* 、/ 、 ~/ 、 %

常用属性: isNaN、isEven、isOdd (是否非数字、奇偶)

常用方法:abs()、round()、floorl()、ceil()、toInt()、toDouble()

    nan ->  not a number  只有这种情况才是非数字

string 字符串

    String a = '''asdfasdf
    ==asdfasdf''';
    String b = 'aaa \n  bbb';
    String bb = r'aaa \n  bbb';
    print(a);
    print(b);
    print(bb);
    var aaa = 'asdfasdf';
     asdfasdf
         ==asdfasdf
     aaa 
       bbb
     aaa \n  bbb
    String b = 'agggb';
    print(b*5);
    print(b[4]);
    
    I/flutter ( 7429): agggbagggbagggbagggbagggb
    I/flutter ( 7429): b

集合

    var list =[1,"asdfas",'aaaa',false,0.2];
    print(list);
    list[0]="hhh";
    print(list);

    var list2 = const[1,2,3];
    var list3=new List();

    list.forEach(print);
    
    I/flutter (26396): [1, asdfas, aaaa, false, 0.2]
    I/flutter (26396): [hhh, asdfas, aaaa, false, 0.2]
    I/flutter (26396): hhh
    I/flutter (26396): asdfas
    I/flutter (26396): aaaa
    I/flutter (26396): false
    I/flutter (26396): 0.2
    var map={"ad":22,3:"gggg",1:"asd",true:'aa',0.58:'asdf'};
    print(map[true]);
    print(map["ad"]);
    map[1]=false;
    print(map[1]);
    var finalMap = const{1:2};

    map.forEach(myPrint);

    print(map.keys);
    print(map.values);

    var list =['aaa','f',"asd",false,0.85];
    print(list.asMap());
    
    void myPrint(key,value){
        print("key=$key,value=$value");
    }
    
    
    I/flutter (17298): aa
    I/flutter (17298): 22
    I/flutter (17298): false
    I/flutter (17298): key=ad,value=22
    I/flutter (17298): key=3,value=gggg
    I/flutter (17298): key=1,value=false
    I/flutter (17298): key=true,value=aa
    I/flutter (17298): key=0.58,value=asdf
    I/flutter (17298): (ad, 3, 1, true, 0.58)
    I/flutter (17298): (22, gggg, false, aa, asdf)
    I/flutter (17298): {0: aaa, 1: f, 2: asd, 3: false, 4: 0.85}
    dynamic b = 20;
    b="kotlin";

    //list本来就可以存动态类型
    var dList = new List<dynamic>();
    dList.add(1);
    dList.add("bala");
    print(dList);

    var list = new List();
    list.add("asdf");
    list.add(false);
    print(list);

运算符

    int a = 10;
    a ??= 66;
    print(a);
    
    
    var a;
    var b = "kotlin";
    print(a??b);
    
    I/flutter (20501): kotlin

方法

      myPrint(key,value){
        print("key=$key,value=$value");
      }
      
      testReturn1(a, b) => "$a,$b";
    
      var hello = 1;
      testReturn2(a, b) => hello == 1 ? "$a" : "$b";
    print(myPrint(1, value:2));
    print(myPrint1(1, "asdf",1));
    
      //可选命名参数
      myPrint(key, {value,age}) {
        print("key=$key,value=$value,age=$age");
      }
    
      //可选位置参数
      myPrint1(key, [String value,aa]) {
        print("key=$key,value=$value,aa=$aa");
      }
    print(myPrint1(1, "asdf",1));
    
    //可选位置参数
    myPrint1(key, [value=10,aa]) {
        print("key=$key,value=$value,aa=$aa");
    }
    var fun01 = myPrint;
    var fun02 = myPrint(1, value:2);
    Function fun03 = myPrint(1, value:2);
    var times01 = times;
    print(listTimes(list02, times01));
        
    String times(str) => str*3;
    
    List listTimes(List list,String times(str)){
        for(int i =0;i<list.length;i++){
            list[i]=times(list[i]);
        }
        return list;
    }
      
    I/flutter (28302): [aaa, bbb, ccc, ddd]
    (str) {
        print("bala~~$str");
    };
    var fun01 = (str) {
      print("bala~~$str");
    };
    fun01("asdf");
    ((){
      print("bbbbbbbbaaaaaa");
    })();
    var list = ["b","a","l","a"];
    
    print(listTimes01(list));
    
    List listTimes01(List list) {
        var func = (str){return str*3;};
        for (int i = 0; i < list.length; i++) {
          list[i] = func(list[i]);
        }
        return list;
    }

闭包

        a(){
    
            int count = 6;
          /*printCount(){
            print(++count);
            }
            return printCount;*/
    
            return (){
                print(++count);
            };
    
        }
        
        var func = a();
        func();
        func();
        func();
        
        I/flutter (28650): 7
        I/flutter (28650): 8
        I/flutter (28650): 9
上一篇 下一篇

猜你喜欢

热点阅读