Flutter 基础语法学习

 

 

 

 

 

 

 

 

 

 

 

void main (List < String > args) {
            print("helloword");
            //单行注释
            /**
             * 多行注释
             *
             */

//变量和数据类型
//Number : int double
            int score = 23;
            double d = 3.4;
            print(score);
            print(d);
//String
            String name = "Hah";
            print(name);
//String 转义
            String s1 = 'i\'m alic';
            String s2 = "i'm alic";
            print(s1);
            print(s2);
//Boolean 
            bool isValid = true;
            print(isValid);

//String 拼接
            String myName = "alic";
            print("my name is" + myName);

//不同类型 不可拼接   字符串和数字不可拼接  ${myName.length}
            print("my name is" + myName.length.toString());

            print("my name is+ ${myName.length}");

            int a = 20;
            int b = 10;
            print("the sum of $a and $b is ${a+b}");
//建议尽量使用$  "my name is $myName"

//分支 if  switch
            var salary = 2000;
            if (salary > 2000) {
                print("$salary > 2000");
            } else if (salary < 2000) {
                print("$salary < 2000");
            } else {
                print("$salary = 2000");
            }

            int c = 2;
            int e = 3;
            if (c < d) {
                print("$c < $e");
            } else {
                print("$c > $e");
            }

//?:三目运算
            a $e");

//?? 表达式  如果为空 取问号后面指
            String name1 = "alic";
            String nameToPrint = name1 ??"333333";
            print(nameToPrint);

            String name2 = null;
            String nameToPrint2 = name2 ??"333333";
            print(nameToPrint2);

//switch

            String grade = "A";
            switch (grade) {
                case "A":
                    print("good");
                    break;
                case "B":
                    print("normal");
                    break;

            }

//for 循环
            for (var i = 0; i < 33; i++) {
                print(i);
            }

            List people =["a", "b", "c"];
            for (var i = 0; i < people.length; i++) {
                print(people[i]);
            }

            //for in
            for (String p in people){
                print(p);
            }

            //while
            int i = 0;
            while (i < people.length) {
                print(people[i]);
                i++;
            }

//break
            for (var i = 0; i < 33; i++) {
                if (i == 5) {
                    break;
                }
                print(i);
            }

//指定停止某个循环
            for (var i = 0; i < 9; i++) {
                inner:
                for (var j = 0; j < 9; j++) {
                    print("$i $j");
                    if (i == 2 && i == 3) {
                        break inner;
                    }
                }
            }

            sum();
            sum1(3, 45);
            print(sum2(44, 888));
            // sum3(3,2);

            // printM(3,width:4);

//try catch finally
            try {
                int res = 12 ~ / 0;
                print(res);
            } catch (e) {
                print(" 异常了");
            } finally {

            }
//  depositMoney(-1);

        }
//函数 具有一定功能的代码块
        void sum () {
            int a = 33;
            int b = 44;
            print("a + b = ${a+b}");
        }

        void depositMoney ( int amount){
            if (amount <= 0) {
                throw new DepositException();
            }
        }
        void sum1 ( int a, int b){
            print("a + b = ${a+b}");
        }

        int sum2 ( int a, int b){
            return a + b;
            //无返回值 函数
            // int sum3(int a,int b){
            //     int sum = a+b;
            //   }

//箭头函数 无返回值
            // void sum4(int a , int b) => print(a+b);
//箭头函数 有返回值
            // int sum4(int a , int b) => print(a+b);

//函数参数 可变
            // void printV(int a ,[int b]){
            //   print(a);
            // }

            //函数参数命名 
            //  void printM(int a ,{int b}){
            //   print(a);
            // }

            //函数参数 赋默认值
            // void printE(int a ,int b=33){
            //   print(a);
            // }


        }

        class DepositException implements Exception {
            String errorMsg() {

                return "金额不能小于0";
            }
        }

 

你可能感兴趣的:(Flutter 基础语法学习)