docs

TOP(About this memo)) > 一覧(Dart) > オブジェクト

オブジェクト指向

identical

expect(identical(Object(), Object()), false);

==メソッド

Object.runtimeType

型テスト演算子

as

is

サンプルコード

void main() {
  print(Person == Person); // true
  print(Person == Taro); // false

  final taro = Taro();
  print(taro.runtimeType == Taro);   // true

  final Person p1 = Taro();
  print(p1.runtimeType == Person);  // false
  print((p1 as Person).runtimeType == Person);  // false

  // オブジェクトではないためisでの比較はfalseとなる
  print(p1.runtimeType is Taro);   // false
  print(p1.runtimeType is Person);  // false

  print(taro is Taro);  // true。常に真のため警告が表示される
  print(taro is Object);  // true。常に真のため警告が表示される
  print(taro is Person);  // true。常に真のため警告が表示される
  print(10 is Object);     // true。常に真のため警告が表示される
  print(null is Object);   // false(Dart 2.12 より前は false)
}
class Person{}
class Taro extends Person{}

print

(参考)pretty print