dart - 转换子类型失败
问题描述
class A {
void hello() {
print('world');
}
}
class B extends A {
@override
void hello() {
print('StackOverflow');
}
}
(A() as B).hello();
结果type 'A' is not a subtype of type 'B' in type cast
。
解决方案
强制转换的工作方式是您只能从更具体的类型(在这种情况下为 B)转换为更通用的类型 A。您创建了 A 的实例,但 A 不是 B。
推荐阅读
- r - 在辅助 R 函数中使用 `subset()`
- visual-studio-code - 资源管理器菜单中的 VSCode“使用代码打开”引发错误
- python-3.7 - 用于在机器之间发送消息的套接字
- python - 如何为 django admin 中的字段提供“添加”选项?
- html - 有没有办法对 Alexa 进行编程以从通过说“Alexa,服务器状态是什么?”指定的页面读取 html 文本?
- python - 修复用于制定素数列表的初学者代码
- linux - 在linux内核模块中获取全局网络命名空间的实例
- java - Firebase .indexOn 未按预期工作
- angular - 订阅管道检测到更改时执行代码
- python-3.x - 当 Scraping 得到带有“编码”部分的 html 时,是否有可能得到它