问题描述
是不是好来初始化AS3类在类的构造函数的变量?或者,我可以只初始化它们的默认值,当我宣布他们在我班上名列前茅?我问,因为当有大量的类变量,它宣布他们在一个地方,然后初始化它们在其他的时候,我可以很容易地在同一个地方做到既显得效率低下。这一个选项是比其他的,为什么呢?
更好谢谢!
例如:
在初始化构造
类Foo {
VAR条:布尔
}
函数foo():无效{
巴= TRUE;
}
或
通过声明初始化
类Foo {
VAR条:布尔=真;
}
函数foo():无效{
}
我个人建议的初始化构造函数中的有两个原因:
- 您不能初始化依赖于其他对象的无功的有已经在施工前创建的对象。
- 它更容易找到和理解初始化code时,它的正常程序。尤其是当你(我)潜水回大类我还没有开了几个月。
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 {
}
I personally recommend initialization inside the constructor for two reasons:
- You cannot initialize objects that depend on other var objects having already been created before construction.
- It's easier to locate and understand initialization code when it's properly procedural. Especially when you (I) dive back into big classes I haven't opened for months.
这篇关于在哪里"适当"地方初始化AS3类变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!