Dart 查看类型的实例是否继承自其他类型

Ale*_*x-c 5 dart

class a {/*code*/}
class b extends a {/*more code*/}
void main() {
  b c = new b();
  print(c.runtimeType == a); //type == inheritedType
  //or
  print(c.runtimeType inherits a); //operator for inheritance
  //or
  print(c inherits a); //other operator for inheritance
}
Run Code Online (Sandbox Code Playgroud)

你能用type == inheriting type, type inherits other type, 或instance inherits other type or type吗?有没有办法做这样的事情?因为我还没有找到任何方法。

Har*_*sen 7

使用is.

你可以做到c is a。请注意,new a() is a也是true如此。如果你真的想知道一个实例是否是另一种类型的子类型,你可能会问c is a && c.runtimeType != a.