使用构造函数“ className()和className ._() [英] The difference between the use of constructor " className() and className._()

查看:138
本文介绍了使用构造函数“ className()和className ._()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种编写构造器的方法。 className() className ._()

I have these two ways to write a constructor. className() and className._()

它们之间有什么区别,什么时候应该使用它们?

What is the difference between them and when should I use which?

 class GlobalState{

      final Map<dynamic,dynamic> _data=<dynamic,dynamic>{};
      static GlobalState instance = new GlobalState._();

      GlobalState._();
    }
    //In Main Class 
       GlobalState _store=GlobalState.instance;

    and 

    class GlobalState{

      final Map<dynamic,dynamic> _data=<dynamic,dynamic>{};
      static GlobalState instance = new GlobalState();
    }
    //In Main Class 
       GlobalState _store=GlobalState();


推荐答案

例如:

class MyClass {
  MyClass();

  MyClass.named();

  MyClass._private();

  MyClass._();
}

上面的类有四个构造函数:

The above class has four constructors:


  • MyClass():这是 default 的未命名构造函数。在设计类时,您希望它表示人们经常使用 的构造函数。

  • MyClass.named():这是一个命名构造函数。也许您希望您的类具有多个构造函数,以便可以用不同的方式构造您的类的实例。 Dart不支持重载函数,因此,如果您需要多个构造函数,则它们需要不同的名称。因此,如果您需要多个构造函数,则需要命名其中一些以区分它们。即使您没有多个构造函数,也可能想要一个明确的名称以避免歧义。

  • MyClass._private() :在Dart中,以下划线开头的名称为 private (通常为文件名)。 MyClass._private()只是一个命名构造函数,不能在其定义的源文件之外使用。通常,当类想要强制调用者时,您会看到私有构造函数使用公共工厂构造函数(或静态方法)来获取实例,尤其是在类要具有单个实例的情况下。

  • MyClass ._():这等效于 MyClass._private()。它不是默认的构造函数,仍然是一个私有的命名构造函数(即,调用者必须使用 MyClass ._()来调用它)。在这种情况下,班级作者想要一个私有的构造函数,但又不想选择一个名称。 (命名很难。)

  • MyClass(): This is the default, unnamed constructor. When you're designing the class, you'd want this to signify that it's the constructor that people should use most of the time. Using it is more convenient because it involves less typing.
  • MyClass.named(): This is a named constructor. Maybe you want your class to have multiple constructors so that an instance of your class can be constructed in different ways. Dart doesn't support overloaded functions, so if you want multiple constructors, they need different names. Hence, if you want multiple constructors, you'll need to name some of them to distinguish between them. Even if you don't have multiple constructors, you might want an explicit name anyway to avoid ambiguity.
  • MyClass._private(): In Dart, names that start with an underscore are private (usually to the file). MyClass._private() is just a named constructor that can't be used outside of the source file it's defined in. Typically you see private constructors when the class wants to force callers to use a public factory constructor (or static method) to get instances, particularly if the class wants to have a single instance.
  • MyClass._(): This is equivalent to MyClass._private(). It's not a default constructor, and it's still a private, named constructor (i.e., callers must use MyClass._() to invoke it). It's a case where the class author wanted a private constructor but didn't feel like choosing a name. (Naming things is hard.)

这篇关于使用构造函数“ className()和className ._()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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