覆盖 Adobe Flex 中 MXML 组件中的 setter
我试图覆盖标签组件中的文本设置器,但它的行为很奇怪 有时 super.text 有效,有时无效!痕迹表明我的代码没有错误。
这是我的代码:
import spark.components.Label;
public class LabelXX extends Label
{
private var _initialText:String;
private var _assignedText:String;
public function LabelXX()
{
super();
}
override public function set text(value:String):void
{
if (!_initialText)
{
_initialText = value;
super.text = value;
trace("initial text = " + value);
}else
{
_assignedText = value;
super.text = _initialText + " " + _assignedText;
trace("Now: " + _initialText + " " + _assignedText);
}
//this wont have any effect no matter what I do:
//super.text = "test test test";
}
override public function get text():String
{
if (!_assignedText)
{
return "";
}else
{
return _assignedText;
}
}
}
更新:如果我评论吸气剂,它工作正常,但仍然没有任何意义!
谢谢
I am trying to override the text setter in the Label component but it behaves weirdly
sometime super.text works and sometimes not! and the traces show that there's no error with my code.
here's my code:
import spark.components.Label;
public class LabelXX extends Label
{
private var _initialText:String;
private var _assignedText:String;
public function LabelXX()
{
super();
}
override public function set text(value:String):void
{
if (!_initialText)
{
_initialText = value;
super.text = value;
trace("initial text = " + value);
}else
{
_assignedText = value;
super.text = _initialText + " " + _assignedText;
trace("Now: " + _initialText + " " + _assignedText);
}
//this wont have any effect no matter what I do:
//super.text = "test test test";
}
override public function get text():String
{
if (!_assignedText)
{
return "";
}else
{
return _assignedText;
}
}
}
UPDATE: if I comment the getter it works fine which still doesn't make any sense!
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最有可能的是超级 get/set 方法正在访问一些保存属性值的私有变量。您的扩展类无权访问此私有变量。通过重写 get 方法从不调用 super;在 set 方法中使用它时,您可能会遇到奇怪的情况。特别是如果父级中的任何内容正在访问私有变量并绕过 get/set 方法。遗憾的是,这种情况偶尔会发生在 Flex 框架代码中。因此,我可能会尝试将您的代码重新编写为如下所示:
Most likely the super get/set method are accessing some private variable that holds the value of the property. You're extended class has no access to this private variable. By overriding the get method to never call super; while using it in the set method, you may experience oddities. Especially if anything in the parent is accessing the private variable and bypassing the get/set methods. It--sadly--happens occasionally in Flex Framework code. So, I might try to re-work your code to something like this: