我可以访问子类'subClass的静态变量吗?我不想实例化它,只是想获取变量,因为它已经被subClass初始化了。
示例:
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;
}
}
谢谢!
答案 0 :(得分:1)
要访问公共静态 var,您可以通过Class.property从同一个包中的任何位置执行此操作。
因此,要访问在C类中定义的MYVAR,您将使用C.MYVAR。
您不需要创建类的实例来访问其中的静态属性或函数 - 只要它们是公共的。
我不建议使用静态,但在极少数情况下,例如“实用程序”类或类似的东西。一个很好的例子是内置的Math类。
我主要在一个包含常量的类中使用static,如下例所示:
package
{
public class Elements
{
public static const FIRE:String = "FIRE_ELEMENT";
public static const WATER:String = "WATER_ELEMENT";
public static const ICE:String = "ICE_ELEMENT";
}
}
然后我会这样使用它:
var myelement:String = Elements.WATER;
答案 1 :(得分:1)
这个问题已经接受了答案,但我的答案是回答Tim Joyce在他对已接受答案的评论中提出的问题:
感谢Marty的回答,什么 关于从C调用public var 不是静止的吗?这可能吗?什么 我有一个从B派出的事件 让我知道C改变了但是我 需要在C中检索变量 发送事件时从A开始。 男人,我希望你能理解什么 我刚写了。
以下是我为回答这个问题而创建的小型Flash应用程序:
package
{
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite
{
private var _popcornMicrowave:PopcornMicrowave;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// 1 minute and 30 seconds
var delay:Number = PopcornMicrowave.MINUTE + (PopcornMicrowave.SECOND * 30);
_popcornMicrowave = new PopcornMicrowave(delay);
trace(_popcornMicrowave.popcorn.isReady); // output: false
_popcornMicrowave.startTimer();
_popcornMicrowave.addEventListener(Event.COMPLETE, onPopcornMicrowaveComplete);
}// end function
private function onPopcornMicrowaveComplete(e:Event):void
{
_popcornMicrowave.removeEventListener(Event.COMPLETE, onPopcornMicrowaveComplete);
trace(_popcornMicrowave.popcorn.isReady) // output: true
}// end function
}// end class
}// end package
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.TimerEvent;
import flash.utils.Timer;
internal class PopcornMicrowave extends EventDispatcher
{
public static const SECOND:Number = 1000;
public static const MINUTE:Number = SECOND * 60;
private var _popcorn:Popcorn;
private var _timer:Timer;
public function get popcorn():Popcorn { return _popcorn }
public function PopcornMicrowave(delay:Number = MINUTE)
{
_popcorn = new Popcorn();
_timer = new Timer(delay, 1);
}// end function
public function startTimer():void
{
if (!_timer.running)
{
_timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
_timer.start();
}// end if
}// end function
private function onTimerComplete(e:TimerEvent):void
{
_timer.removeEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
_popcorn.setIsReady(true);
dispatchEvent(new Event(Event.COMPLETE));
}// end function
}// end class
internal class Popcorn
{
private var _isReady:Boolean;
public function get isReady():Boolean { return _isReady }
public function Popcorn() { }
internal function setIsReady(boolean:Boolean):void
{
_isReady = boolean
}// end function
}// end class
答案 2 :(得分:1)
可以从任何地方轻松引用静态变量。无论是访问其中一个子类(或子子类)的超类,还是依赖于相关静态变量的完全不同的类,您都可以这样访问它:
public function someFunction():void {
//Direct Access: Most typical way - accessing a public static var / const,
//your local-variable should match the data-type of the static
//variable you're accessing.
var localVar:String = ClassName.StaticVarName;
//Dynamic Access: An alternative that can be handy for verifying
//variables that exists, and then use them once confirmed:
if(ClassName["StaticVarName"]!=null) {
trace("Found value: " + ClassName["StaticVarName"]);
}
//Iterative Access: If you're looking to iterate through each static
//variables of a certain class, you can collect those by accessing its
//description XML document (E4X access that is).
var classDescription:XML = describeType(ClassName);
var classVarNames:XMLList = classDescription.variable.attribute("name");
for each(var classVarName:String in classVarNames) {
trace("Found class var: " + classVarName);
}
}
现在,为了访问类层次结构中隐藏了几层的实例变量,您可以尝试这样的事情:
// File: A.as
package {
public class A {
public function A() {
}
public function doSomething():void {
switch(Object(this).constructor) {
case B: trace( B(this).methodOnlyInB() ); break;
case C: trace( C(this).variableOnlyInC ); break;
default: trace("You are simply in 'A'!"); break;
}
}
}
}
// File: B.as
package {
public class B extends A {
public function B() {
super();
}
public function methodOnlyInB():String {
return "You are calling a 'B' method!";
}
}
}
// File: C.as
package {
public class C extends B {
public var variableOnlyInC:String = "You are tracing a 'C' variable";
public function C() {
super();
}
}
}
// File: TestABC.as
var a:A = new A();
var b:B = new B();
var c:C = new C();
a.doSomething(); //Should trace: You are simply in 'A'!
b.doSomething(); //Should trace: You are calling a 'B' method!
c.doSomething(); //Should trace: You are tracing a 'C' variable!
这条路线有一点“问题”。这会让你写“糟糕的代码”。超级班级应该不知道它的任何子类业务以及它们在某些条件下应该做什么。它的规则几乎每个编程设计模式都试图鼓励(并且有充分的理由),并且违反这一规则从长远来看会花费大量的时间,精力和头发。
这样的情况需要“ Controller ”类。基本上,从其他地方实例化的类将负责分析A / B / C对象(例如:类似于A.doSomething()的switch语句如何执行此操作)。这可以释放你的Super&来自杂乱的子类,为扩展和可靠的代码留下了更多的空间。
希望这能满足您所有的变量访问需求! :)