Dart12运算符

1.算术运算符

print(2 + 3 == 5);
print(2 - 3 == -1);
print(2 * 3 == 6);
print(5 / 2 == 2.5); // 结果是双浮点型
print(5 ~/ 2 == 2); // 结果是整型
print(5 % 2 == 1); // 余数

2.级联运算符

级联(..)运算符可用于通过对象发出一系列调用。

class Student { 
   void test_method() { 
      print("This is a  test method"); 
   } 

   void test_method1() { 
      print("This is a  test method1"); 
   } 
}  
void main() { 
   new Student() 
   ..test_method() 
   ..test_method1(); 
}

执行上面示例代码,得到以下结果 -

This is a test method 
This is a test method1

3.类型判定符

符号描述
as将对象强制转换为特定类型
is类型判定
is!类型判定

示例

class Person {
  String name = "jack";
}

void main() {
  var p = Object();
  // p = Person();
  // print(p.name);
  // print((p as Person).name);
  if (p is Person) {
    print(p.name);
  }
}