“合适的"在哪里?在 AS3 中初始化类变量的地方 [英] Where is the "proper" place to initialize class variables in AS3

查看:31
本文介绍了“合适的"在哪里?在 AS3 中初始化类变量的地方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在类构造函数中初始化 AS3 类变量是否更好"?或者我可以在我的班级顶部声明它们时将它们初始化为默认值吗?我问是因为当有很多类变量时,在一个地方声明它们然后在另一个地方初始化它们似乎效率低下,而我可以在同一个地方轻松地做这两个.一种选择比另一种更好,为什么?

Is it "better" to initialize AS3 class variables in the class constructor? Or can I just initialize them to their default value when I declare them at the top of my class? I ask because when there's a lot of class variables, it appears inefficient to declare them in one place and then initialize them in another when I could easily do both in the same place. It one option is better than the other, why?

谢谢!

例如:

在构造函数中初始化

class foo {
    var bar:Boolean
}

function foo():void {
    bar = true;
}

用声明初始化

class foo {
    var bar:Boolean = true;
}

function foo():void {
}

推荐答案

我个人推荐在构造函数内部初始化,原因有两个:

I personally recommend initialization inside the constructor for two reasons:

  1. 不能初始化依赖于在构造之前已经创建的其他 var 对象的对象.
  2. 如果程序正确,则更容易定位和理解初始化代码.尤其是当你(我)重新投入我几个月没有开设的大班级时.

这篇关于“合适的"在哪里?在 AS3 中初始化类变量的地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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