Dart立即或在构造函数中分配给变量? [英] Dart assigning to variable right away or in constructor?

查看:138
本文介绍了Dart立即或在构造函数中分配给变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Dart中,立即赋值与在Java中像在构造函数中赋值一样?

In Dart, is there a difference in assigning values right away vs in constructor like in Java?

class Example {
    int x = 3;
}

vs

class Example {
    int x;
    Example() {
        x = 3;
    }
}

我问,因为当我使用Flutter并尝试将使用setState的Function分配给变量时,前一种方法是不可能的,而后者则是可能的.

I ask because when I was using Flutter and tried to assign a Function that uses setState to a variable, it was not possible with the former method but possible with the latter.

推荐答案

在您的琐碎情况下,没关系.

In your trivial case, it doesn't matter.

通常,您可以通过以下几种方式初始化实例变量:

In general, you can initialize instance variables in a few ways:

class Example1 {
  T x = value;
}

优势:

  • 简洁明了.
  • 成员将在 all 构造函数中初始化.
  • 可用于初始化final成员.
  • 在调用基类构造函数之前初始化成员,如果基类构造函数调用被派生类覆盖的成员函数,这可能很重要.
  • Direct, concise.
  • Member will be initialized in all constructors.
  • Can be used to initialize final members.
  • Member is initialized before invoking base class constructors, which can matter if the base class constructor calls member functions that are overridden by the derived class.

缺点:

  • 不能依赖于构造参数.
  • 不能依赖this,因为初始化发生在this生效之前.
  • Can't depend on construction arguments.
  • Can't depend on this since the initialization occurs before this becomes valid.
class Example2 {
  T x;

  Example2() : x = value;
}

优势:

  • 可用于初始化final成员.
  • 在调用基类构造函数之前初始化成员,如果基类构造函数调用被派生类覆盖的成员函数,这可能很重要.
  • 可以利用构造参数.
  • Can be used to initialize final members.
  • Member is initialized before invoking base class constructors, which can matter if the base class constructor calls member functions that are overridden by the derived class.
  • Can utilize construction arguments.

缺点:

  • 如果该类具有多个构造函数,则需要重复初始化,否则构造函数应重定向到公共构造函数.
  • 不能依赖this,因为初始化发生在this生效之前.
  • If the class has multiple constructors, initialization would need to be duplicated, or constructors should redirect to a common constructor.
  • Can't depend on this since the initialization occurs before this becomes valid.
class Example3 {
  T x;

  Example3() {
    x = value;
  } 
}

优势:

  • 可以利用构造参数.
  • 可用于执行更复杂的初始化,例如无法通过单个表达式初始化成员的情况.
  • 可以使用this.

缺点:

  • 不能用于初始化final成员.
  • 如果该类具有多个构造函数,则将需要重复初始化或需要重构初始化代码(例如但不限于重定向到通用构造函数).
  • 在调用基类构造函数后在 初始化成员.
  • Cannot be used to initialize final members.
  • If the class has multiple constructors, initialization would need to be duplicated or initialization code would need to be refactored out (such as, but not limited to, redirecting to a common constructor).
  • Member is initialized after invoking base class constructors.

我可能忘记了一些要点,但我认为这应该涵盖主要要点.

There probably are some points I'm forgetting, but I think that should cover the main ones.

直接内联初始化首先发生,然后是初始化列表,然后是构造函数主体.另请参见在参数列表和初始化程序列表中分配值之间的区别,其中解释了为什么this仅在以下阶段才有效对象初始化.

Direct, inline initialization occurs first, then initialization lists, then constructor bodies. Also see Difference between assigning the values in parameter list and initialiser list which explains why this becomes valid only for the later stages of object initialization.

这篇关于Dart立即或在构造函数中分配给变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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