“正确”的地方在哪里? AS3中初始化类变量的地方
在类构造函数中初始化 AS3 类变量是否“更好”?或者当我在类的顶部声明它们时,我可以将它们初始化为默认值吗?我问这个问题是因为当有很多类变量时,在一个地方声明它们然后在另一个地方初始化它们似乎效率很低,而我可以轻松地在同一个地方执行这两个操作。其中一种选择比另一种更好,为什么?
谢谢!
例如:
在构造函数中初始化
class foo {
var bar:Boolean
}
function foo():void {
bar = true;
}
或
使用声明初始化
class foo {
var bar:Boolean = true;
}
function foo():void {
}
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?
Thanks!
eg:
initializing in constructor
class foo {
var bar:Boolean
}
function foo():void {
bar = true;
}
OR
initializing with the declaration
class foo {
var bar:Boolean = true;
}
function foo():void {
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我个人建议在构造函数内进行初始化,原因有两个:
I personally recommend initialization inside the constructor for two reasons:
我个人也不这么做!我喜欢创建一个“init”函数来初始化所有内容。这意味着如果我需要将实例重置为其默认值,我可以随时调用 init 方法。
Personally I don't do either! I like to create an "init" function that initializes everything. That means if I need to reset the instance to its default, I can just call the init method at anytime.