如何检查开关/箱中对象的类型? [英] How do I check type of an object in a switch/case?

查看:51
本文介绍了如何检查开关/箱中对象的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在示例中看到以下代码:

I see in examples this code:

if (obj is User) { // do something }

我想检查开关/案例流中对象的类型,并找到一个属性 .runtimeType :

I want to check the type of the object in a switch/case flow, and found a property .runtimeType of the object:

switch (obj.runtimeType) {
    case User:
        print(obj);
        break;
}

这是检查对象类型的正确方法吗?

Is this a correct way to check the type of the object?

推荐答案


Dart switch语句中不支持按类型进行切换。
如果使用测试,则应使用的顺序:

There is no support in the Dart switch statement for switching by type. You should use a sequence of if tests instead:

if (obj is User) { 
  print(obj);
} else if (obj is ...) ...

我真的,真的建议不要使用 runtimeType

I really, really recommend that you never use runtimeType for anything.

使用 dart:mirrors ,关于对象的类型(但您也可以只使用 reflect(object)反映对象本身)。除此之外,使用 runtimeType 几乎总是会引起其他可以避免的问题。

It can be used to reflect, using dart:mirrors, on the type of an object (but you can also just use reflect(object) to reflect on the object itself). Apart from that, using runtimeType almost always causes otherwise avoidable problems.

唯一的< runtimeType 返回的code> Type 对象用于检查其是否相等。如果这样做(如上面的开关中所示),则说明您无法正确处理子类型。如果您的系统中曾经有 User 的子类型,例如 User 是接口并且实现类是不同的,或者如果您模拟 User 进行测试或其他多种原因,则该实例将没有 User runtimeType ,代码将无法识别它。
也许会这样,因为 runtimeType 可以被用户覆盖,并且任何类都可以选择返回 User 。对 runtimeType 进行测试并不能保证该类实际上是您要检查的类。

The only thing you can do with the Type object returned by runtimeType is to check it for equality. If you do that (like in the switch above), then you don't correctly handle subtypes. If you ever have a subtype of User in your system, like if User is an interface and the implementation class is different, or if you mock a User for testing, or any number of other causes, then the instance will not have User as runtimeType, and the code won't recognize it. Or maybe it will, because runtimeType can be overridden by the user, and any class can choose to return User. Testing against runtimeType isn't a guarantee that the class is actually the one you check for.

比较时对于类型,应该始终使用 is ,因为它可以正确处理子类。
子类型可替代性是面向对象编程的核心思想之一,因此可以说,如果使用 runtimeType ,则可能不是在进行最佳的OO设计。

When you compare for type, you should always use is because it correctly handles subclasses. Subtype substitutability is one of the core ideas of object oriented programming, so you could say that if you use runtimeType, you are probably not doing an optimal OO design.

(在某些情况下,代码在 operator =中使用 other.runtimeType == MyClass = 避免与子类实例相等,从而避免 ColorPoint问题-但这意味着无法制作该类型的子类或接口实现(包括模拟)并使其通过相等性检查,这是我在共享库代码中应避免的非常微妙的限制,您在自己的应用程序中所做的事情,至少没有其他人会依赖,这仅仅是您的问题:smile:)。

(There are cases where code uses other.runtimeType == MyClass in operator== to avoid being equal to a subclass instance to avoid the "ColorPoint" problem - but it means that it's impossible to make a subclass or interface implementation (which includes mocks) of that type and have it pass equality checks. That's a very subtle restriction that I would avoid in shared library code. What you do in your own application, that nobody else will depend on, is at least only your problem :smile:).

这篇关于如何检查开关/箱中对象的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆