[英]Have dart class public, but constructor private
I am wondering if there is a way to have a dart class be public, but its contructor be library private. 我想知道是否有办法让dart类公开,但它的构造函数是库私有的。 I have a JsonParsing library that will throw its own exception type, but it doesn't make sense for anyone outside of the library to construct this exception. 我有一个JsonParsing库,它将抛出自己的异常类型,但是对于库之外的任何人构造此异常没有意义。
library json_parser;
import 'dart:convert';
class JsonParseException implements Exception {
final String cause;
JsonParseException(this.cause); // make this inaccessible in some way?
}
You can use a private named constructor: 您可以使用私有命名构造函数:
class Foo {
Foo._();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.