AS3从父类引用子类的子类而不实例化?
我可以访问子类的子类的静态变量吗?我不想实例化它,只是想获取变量,因为它已经由子类初始化了。
例如:
package
{
public class A extends MovieClip {
private var classB:B = new B();
public function A() {
//**ACCESS B.C.MYVAR**
}
}
}
package
{
public class B extends MovieClip {
private var classC:C = new C();
}
}
package
{
public class C extends MovieClip {
public static var MYVAR = 1;
}
}
谢谢!
Can I access a static variable of a subClass' subClass? I don't want to instantiate it, just looking to get the variable because it has already been initialized by the subClass.
example:
package
{
public class A extends MovieClip {
private var classB:B = new B();
public function A() {
//**ACCESS B.C.MYVAR**
}
}
}
package
{
public class B extends MovieClip {
private var classC:C = new C();
}
}
package
{
public class C extends MovieClip {
public static var MYVAR = 1;
}
}
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要访问 public static var,您可以通过 Class.property 从同一包中的任何位置执行此操作。
因此,要访问 C 类中定义的 MYVAR,您可以使用 C.MYVAR。
您不需要创建类的实例来访问其中的静态属性或函数 - 只要它们是公共的即可。
我不建议使用静态,除非在极少数情况下,例如“实用程序”类或类似的东西。一个很好的例子是内置的 Math 类。
我主要在保存常量的类中使用 static ,就像这个例子:
然后我像这样使用它:
To access a public static var you can do so from anywhere within the same package via Class.property.
So to access MYVAR that's defined in Class C, you would use C.MYVAR.
You don't need to create an instance of a class to access static properties or functions within it - so long as they are public.
I don't recommend using static except for in rare cases such as a "utilities" class or something similar. A good example is the Math class that's inbuilt.
I mostly use static in a class that holds constants, like this example:
Then later I make use of this like so:
这个问题的答案已经被接受,但我的回答是为了回应蒂姆·乔伊斯在对已接受答案的评论中提出的问题:
以下是我为回答该问题而创建的一个小型 Flash 应用程序:
An answer has already been accepted for this question but my answer is in response to the question asked by Tim Joyce in his comment for the accepted answer:
The following is a small flash application I created to answer that question:
静态变量可以轻松地从任何地方引用。无论它是访问其子类之一的超类(或子子类...类),还是依赖于相关静态变量的完全不同的类,您都可以通过以下方式访问它:
现在,在为了访问隐藏在类层次结构中几层的实例变量,您可以尝试如下操作:
沿着这条路线走下去有一点“问题”。这会让你写出“糟糕的代码”。超类不应该知道它的任何子类业务以及它们在某些条件下应该做什么。这是几乎所有编程设计模式都试图鼓励的规则(并且有充分的理由),从长远来看,违反这条规则可能会花费大量的时间、精力和麻烦。
像这样的情况需要“Controller”类。基本上,从其他地方实例化的类将负责分析 A/B/C 对象(例如:类似于 A.doSomething() 的 switch 语句执行此操作的方式)。这可以释放您的 Super &子类避免了混乱,并为扩展和可靠的代码留下了更多的空间。
希望这能满足您所有的变量访问需求! :)
A static variable can easily be referenced from anywhere. Whether it'd be a super-class accessing one of it's subclass (or sub-sub-...-class), or a completely different class dependent on the static variable in question, you can access it this way:
Now, in order to access instance variables buried a few levels down in your Class hierarchy, you can try something like this:
There is one bit of "issue" going down this route. This will make you write "poor code". A Super-Class should NOT know any of it's sub-classes business and what they should do under certain conditions. Its a rule that pretty much every programming design pattern try to encourage (and for good reasons), and violating this rule can cost a lot of time, energy and hair-pulling in the long run.
Situations like this calls for a "Controller" class. Basically a class, instantiated from elsewhere will have the responsibility to analyze the A/B/C object (ex: similarly to how A.doSomething()'s switch statement does this). This frees up your Super & Sub classes from clutter and leaves much more room for expansion and solid code.
Hope this answers all your variable access needs! :)