• Dart学习记录(五)


     1、? 可空类型 类型值后加?
    String? username="张三";   // String?  表示username是一个可空类型
    username=null;
    print(username);
     List<String>? l1=["张三","李四","王五"];
     l1=null;  
      print(l1);
     
       2、 ! 类型断言
     String? str = "this is str";
    
      str = null;
    
      print(str!.length);
    
      //类型断言: 如果str不等于null 会打印str的长度,如果等于null会抛出异常
    
      printLength("str");

      3、 late 关键字 

        用于延迟初始化

    class Person {
      late String name;
      late int age;
    
      void setName(String name, int age) {
        this.name = name;
        this.age = age;
      }
    
      String getName() {
        return "${this.name}---${this.age}";
      }
    }
    
    void main(args) {
      Person p = new Person();
      p.setName("张三", 20);
      print(p.getName());
    }

      4、required关键词 表明参数必须传入

    String printInfo(String username, {required int age, required String sex}) {//行参    
      return "姓名:$username---性别:$sex--年龄:$age";
    }
    class Person {
      String name;
      int age;
      Person({required this.name,required this.age});  //表示 name 和age 必须传入
    
      String getName() {
        return "${this.name}---${this.age}";
      }
    }
  • 相关阅读:
    hdu 1087(LIS变形)
    poj 1088(记忆化搜索)
    hdu 1505(最大子矩阵)
    hdu 1506(好题+DP或者RMQ)
    poj 2593&&poj2479(最大两子段和)
    hdu 1003(最大子段和)
    hdu 2881(LIS变形)
    poj 1692(动态规划)
    CodeForces 626C Block Towers
    CodeForces 626B Cards
  • 原文地址:https://www.cnblogs.com/w-sansamilly/p/15014163.html
Copyright © 2020-2023  润新知