Dart语言学习(九) 运算符

一、运算符及其描述

 二、Dart运算符注意点

1. 除法运算符"/" 和 整除运算法"~/" 的区别

除法运算符 "/"  结果是浮点型

整除运算法 "~/" 结果是整型,并且是舍去小数部分。

如下面代码:

  int a = 9;
  int b = 2;
  print(a / b);
  print(a ~/ b);

输出:

4.5
4

2.类型测试操作符

as、is 和 is! 操作符在运行时用于检查类型很方便

is 、as 属于Type test operators

is 判断是否是某个类型,返回true或者false。
如果a 是b的实现类,那么a is b 就返回true。

is! 如果a 不是 b的实现类,那么a is!b 就返回true。

as 是类型转换,也就是: "先检测 其是不是,然后再调用" 的简写
但是还不是完全一致,当不是的时候,as 会抛出exception

  Person person = new Student();
  person.name = "Tom";
  person.age = 18;

  if(person is Student){
    person.study();
  }

上述代码的函数调用部分等同于:

(person as Student).study();

 

Dart学习系列文章:https://www.cnblogs.com/jukaiit/category/1636484.html
posted @ 2020-02-03 15:19  鸿鹄当高远  阅读(722)  评论(0编辑  收藏  举报