如何解决在构造函数中声明的变量与类属性冲突
| 我正在通过扩展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(/*..*/);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。