您是否需要使用“新”功能? Dart中的关键字? [英] Do you need to use the "new" keyword in Dart?

查看:112
本文介绍了您是否需要使用“新”功能? Dart中的关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在基于Dart的应用程序中,我刚刚注意到我可以省略 new 关键字,并且一切正常。

In my Dart based application I just noticed that I can omit the new keyword and everything works perfectly fine.

我也可以使用 final widget = Widget(); 来代替 final Widget = new Widget();

Instead of final widget = new Widget(); I can also use final widget = Widget();.

这对代码有影响吗?

推荐答案

不,它不是。使用 Dart 2(单击以获取更多信息的公告) <$将c $ c> new 和 const 关键字设为可选

No, it does not. With Dart 2 (click for the announcement with more information) the new and also const keywords were made optional.

这意味着新Widget()的作用与 Widget()

const 关键字可以将不是 const 的值隐式更改为 const

因此,您需要在需要时明确指定 const

The const keyword can, however, change a value that would not be a const implicitly to a const.
So you will have to explicitly specify const when needed.

这篇关于您是否需要使用“新”功能? Dart中的关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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