简体   繁体   English

这个以 Classname 为前缀的 class-const-Syntax 在 Dart 中是什么意思?

[英]What does this class-const-Syntax prefixed by Classname mean in Dart?

Although I thought being familiar with programming language Dart, i stumbled upon this syntax in an example for Bloc:虽然我认为熟悉编程语言 Dart,但我在 Bloc 的示例中偶然发现了这种语法:

class AuthenticationState extends Equatable {
  const AuthenticationState._({
    this.status = AuthenticationStatus.unknown,
    this.user = User.empty,
  });

  const AuthenticationState.unknown() : this._();

  const AuthenticationState.authenticated(User user)
      : this._(status: AuthenticationStatus.authenticated, user: user);

  const AuthenticationState.unauthenticated()
      : this._(status: AuthenticationStatus.unauthenticated);

  final AuthenticationStatus status;
  final User user;

  @override
  List<Object> get props => [status, user];
}

I know how to define a class constant and a const constructor.我知道如何定义一个 class 常量和一个 const 构造函数。

However, why is the classname prefixed here everywhere?但是,为什么这里到处都是类名前缀呢?

const AuthenticationState._({
    this.status = AuthenticationStatus.unknown,
    this.user = User.empty,
  });

That's a named constructor.那是一个命名的构造函数。 In dart you can define constructors in two ways, either using ClassName or ClassName.someOtherName .在 dart 中,您可以通过两种方式定义构造函数,使用ClassNameClassName.someOtherName

Eg: Consider you have a class called person with 2 variables, name and carNumber.例如:假设您有一个名为 person 的 class 具有 2 个变量,name 和 carNumber。 Everyone has a name but carNumber is not necessary.每个人都有名字,但 carNumber 不是必需的。 In that situation, if you implement the default constructor, you have to initialise it like:在这种情况下,如果你实现了默认构造函数,你必须像这样初始化它:

Person("Name", "");

So if you want to add some syntactic sugar, you can add a named constructor like below:所以如果你想添加一些语法糖,你可以添加一个命名构造函数,如下所示:

class Person {
  String name;
  String carNumber;

  // Constructor creates a person with name & car number
  Person(this.name, this.carNumber);

  // Named constructor that only takes name and sets carNumber to ""
  Person.withOutCar(String name): this(name, "");

  // Named constructor with no arguments, just assigns "" to variables
  Person.unknown(): this("", "");
}

And you can initialise the object like:您可以像这样初始化 object:

Person.withOutCar("Name");

In the above example the named constructors are being redirected to your actual constructor with pre-defined default values.在上面的示例中,命名构造函数被重定向到具有预定义默认值的实际构造函数。

You can read more about named constructors here: Dart Language Tour您可以在此处阅读有关命名构造函数的更多信息: Dart 语言导览

As the above answer was not really clear/complete to me, when I stumbled upon this, please me let me know, whether my understanding is correct:由于上述答案对我来说不是很清楚/不完整,当我偶然发现这个问题时,请告诉我,我的理解是否正确:

const AuthenticationState._({
    this.status = AuthenticationStatus.unknown,
    this.user = User.empty,
  });

The constructor using ._() is private to the library, I guess this is a security feature, so you can create an instance only from within the library.使用._()的构造函数是库私有的,我想这是一个安全功能,所以你只能从库中创建一个实例。

const AuthenticationState.unknown() : this._();

If the object is created as .unknown , the default constructor from above is called (as private), such using the default values.如果 object 创建为.unknown ,则调用上面的默认构造函数(作为私有),例如使用默认值。 In the other 2 cases, one or both default values are replaced.在其他 2 种情况下,替换一个或两个默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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