简体   繁体   中英

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. 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.

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._();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM