飞镖类型转换错误 [英] Dart typecasting error

查看:62
本文介绍了飞镖类型转换错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抽象类 Event 和一个扩展它的具体子类,称为 PlaceChangeEvent 。在事件监听器中,我有以下代码:

I have an abstract class Event and a concrete subclass that extends it called PlaceChangeEvent. Inside an event listener, I have the following code:

void onEvent(Event event) {
    PlaceChangeEvent pce = null;
    if(event is PlaceChangeEvent)
        pce = (PlaceChangeEvent)event;      // <== error is here
    else
        throw new Exception("Event is not a PlaceChangeEvent.");

    Place place = pce.place;

    presenterProvider.display(place);
}

因此,如果运行时类型为 event PlaceChangeEvent ,那么我需要将该事件强制转换为该类型,以便可以访问其属性。但是我在类型转换中遇到编译器错误,指出:

So if the runtime type of event is PlaceChangeEvent, then I need to cast the event to that type so that I can access its properties. But I'm getting a compiler error on the typecast, stating:


类型'Type'的值不能分配给变量键入'PlaceChangeEvent'

A value of type 'Type' cannot be assigned to a variable of type 'PlaceChangeEvent'

我在哪里出错了,我该怎么做才能解决它?

Where am I going wrong, and what do I need to do to fix it?

推荐答案

在Dart中


  1. 上载是隐式的。如果 B A 的子类,则 B b = a (其中 a 是类 A 的实例)免费警告,并且无提示地强制转换 a B 。在Java中,您需要编写 B b =(B)a

  1. upcasts are implicit. If B is a subclass of A, then B b = a (where a is an instance of class A) is warning free and silently casts a to B. In Java you would have needed to write B b = (B) a.

动态类型是永远存在的逃生舱口。如果 B C 不在同一层次结构中,则暂时分配给动态类型将使强制类型转换警告-自由。

the dynamic type is an always-present escape hatch. If B and C are not in the same hierarchy, then temporarily assigning to the dynamic type will make the cast warning-free.

B b = someB;
var tmp = b;
C c = tmp;


  • 一个人可以使用 is 。如果对象的类型正确,则 is 检查将返回true。还有一些传播is-check类型的非常简单的规则。例如,如果在 if 条件内使用了 is ,则相应的分支将使用此信息进行类型警告。

  • One can do an explicit check using is. The is check returns true if the object is of the right type. There are also some very simple rules that propagate is-check types. For example, if is is used inside an if condition, then the corresponding branch uses this information for type-warnings.

    Object o;
    o.foo();  // warning: foo does not exist on Object.
    if (o is A) {  // assuming that A contains 'foo'.
      o.foo();  // warning-free.
    }
    


  • 一个人可以显式检查并抛出是否不是期望的类型一个 as 。当左侧为 null 时,此运算符不会抛出。

  • One can explicitly check and throw if a type is not the expected one with as. This operator does not throw when the left-hand side is null.

    例如,这可以归结为:

    没有显式检查(1):

    void onEvent(Event event) {
      // Implicit upcast. PlaceChangeEvent is subclass of Event.
      PlaceChangeEvent pce = event;
      Place place = pce.place;
      presenterProvider.display(place);
    }
    

    带有is-check(3):

    With an is-check (3):

    void onEvent(Event event) {
      if (event is PlaceChangeEvent) {
        // Static type system propagates type. No need for variable.
        Place place = event.place;
        presenterProvider.display(place);
      } else {
        // Note: this doesn't look like an exception, but an error.
        throw new Exception("Event is not a PlaceChangeEvent.");
      }
    }
    

    使用 as (4):

    void onEvent(Event event) {
      Place place = (event as PlaceChangeEvent).place;
      presenterProvider.display(place);
    }
    

    或者,如果您希望收到PlaceChangeEvent,则只需更改类型参数的值:

    Alternatively, if you expect to receive a PlaceChangeEvent, you should simply change the type of the argument:

    void onEvent(PlaceChangeEvent event) {
      Place place = event.place;
      presenterProvider.display(place);
    }
    

    在选中模式下,它将捕获错误的类型,而在未选中模式下,它将抛出访问 event.place 时。通常,这是首选方式。

    In checked mode this will catch bad types, and in unchecked mode it will throw when accessing event.place. This is generally the preferred way.

    这篇关于飞镖类型转换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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