可以在dart中使用私有构造函数吗? [英] Is it possible to have a private constructor in dart?

查看:1241
本文介绍了可以在dart中使用私有构造函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在TypeScript中执行以下操作

I'm able to do something like the following in TypeScript

class Foo {
  private constructor () {}
}

所以这个构造函数仅可从类内部访问。

so this constructor is accessible only from inside the class itself.

如何在Dart中实现相同的功能?

How to achieve the same functionality in Dart?

推荐答案

只需创建一个以 _

class Foo {
  Foo._() {}
}

然后只能从其类(和库)访问构造函数 Foo ._()

then the constructor Foo._() will be accessible only from its class (and library).

这篇关于可以在dart中使用私有构造函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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