覆盖 Adob​​e Flex 中 MXML 组件中的 setter

发布于 2024-12-21 13:14:48 字数 1083 浏览 1 评论 0原文

我试图覆盖标签组件中的文本设置器,但它的行为很奇怪 有时 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野侃 2024-12-28 13:14:48

最有可能的是超级 get/set 方法正在访问一些保存属性值的私有变量。您的扩展类无权访问此私有变量。通过重写 get 方法从不调用 super;在 set 方法中使用它时,您可能会遇到奇怪的情况。特别是如果父级中的任何内容正在访问私有变量并绕过 get/set 方法。遗憾的是,这种情况偶尔会发生在 Flex 框架代码中。因此,我可能会尝试将您的代码重新编写为如下所示:

private var _initialText:String;
private var _assignedText:String;

    override public function set text(value:String):void
    {
        if (!_initialText)
        {
            _initialText = value;
            super.text = value;
            trace("initial text = " + value);
        }else
        {
            _assignedText = value;
// change here to reference value not _assignedText
            super.text = _initialText + " " + value;
            trace("Now: " + _initialText + " " + value);
        }
        //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
        {
// change here to reference super.text not _assignedText
            return super.text;
        }
    }

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:

private var _initialText:String;
private var _assignedText:String;

    override public function set text(value:String):void
    {
        if (!_initialText)
        {
            _initialText = value;
            super.text = value;
            trace("initial text = " + value);
        }else
        {
            _assignedText = value;
// change here to reference value not _assignedText
            super.text = _initialText + " " + value;
            trace("Now: " + _initialText + " " + value);
        }
        //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
        {
// change here to reference super.text not _assignedText
            return super.text;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文