问题描述
|
我正在通过扩展flash.display.SimpleButton在ActionScript中创建一个按钮
但是,当我在构造函数中声明某些变量时,该按钮的行为也不符合预期,这些变量也恰好作为属性存在于SimpleButton类中。他们似乎发生冲突。
为什么是这样?是否不应该允许本地声明的变量与名称相似的类属性共存?
以下代码片段可能会更好地说明问题:
public class MyButton extends SimpleButton{
public function MyButton(/*..*/){
var upState:ButtondisplayState = new ButtondisplayState(/*..*/));
var downState:ButtondisplayState = new ButtondisplayState(/*..*/);
var overState:ButtondisplayState = new ButtondisplayState(/*..*/);
var hitTestState:ButtondisplayState = new ButtondisplayState(/*..*/);
super(upState,overState,downState,hitTestState);
}
}
api文档位于此处(例如,查找upState):http://livedocs.adobe.com/flash/9.0/ActionScriptLangrefV3/flash/display/SimpleButton.html#upState
谢谢,
奥德
解决方法
您无法重新声明已经存在的变量,无论是否是局部变量。可以执行类似操作的唯一位置是方法参数,在该位置可以具有与本地/类变量相同的参数名称。
为什么不直接将这些状态直接传递给构造函数,如:
super(new ButtonDisplayState(/*..*/)),new ButtonDisplayState(/*..*/)),new ButtonDisplayState(/*..*/)));
或者也可以在调用super();
之后直接设置它们,如下所示:
upState = new ButtonDisplayState(/*..*/));
downState = new ButtonDisplayState(/*..*/);
overState = new ButtonDisplayState(/*..*/);
hitTestState = new ButtonDisplayState(/*..*/);