AS3:覆盖子类中的受保护变量

发布于 2024-08-15 02:09:44 字数 1393 浏览 10 评论 0原文

我这里停电了我以为我理解了这些原则,但我似乎无法再让它发挥作用了。我想让DeleteButton继承一个通用的Button类。此删除按钮应更改受保护的填充值并具有静态标签。这就是我所拥有的:

public class Button
{
    private var _labelText:String;

    protected var _paddingX:Number = 10;
    protected var _paddingY:Number = 5;

    public function Button( labelText:String ):void
    {
        _labelText = labelText;
    }
}

public class DeleteButton extends Button
{
    public function DeleteButton():void
    {
        _paddingX = 5;
        _paddingY = 2;

        super( 'x' );
    }
}

现在,我认为继承类中更改的 _paddingX 和 _paddingY 值会冒泡到超类。但他们没有。 DeleteButton 仍然是使用超类的默认值构造的。我不知道该怎么做了。我在这里缺少什么?

提前致谢。

编辑:

我想我将 AS3 的实现与 PHP 的实现混合在一起了。 PHP 中的这个“等效”示例是完全合法的:

class Button
{

    protected $_paddingX = 10;
    protected $_paddingY = 5;

    public function __construct( $label = '' )
    {
        var_dump( $label . ':' );
        var_dump( $this->_paddingX );
        var_dump( $this->_paddingY );
        echo '<br>';
    }
}

class DeleteButton extends Button
{
    public function __construct()
    {
        $this->_paddingX = 5;
        $this->_paddingY = 2;

        parent::__construct( 'x' );
    }
}

$button = new Button( 'browse' );
$deleteButton = new DeleteButton();

// this outputs:
string(7) "browse:" int(10) int(5) 
string(2) "x:" int(5) int(2)

I'm having a blackout here. I thought I understood these principles, but I can't seem to get it working anymore. I want to let a DeleteButton inherit from a general Button class. This DeleteButton should alter the protected padding values and have a static label. This is what I have:

public class Button
{
    private var _labelText:String;

    protected var _paddingX:Number = 10;
    protected var _paddingY:Number = 5;

    public function Button( labelText:String ):void
    {
        _labelText = labelText;
    }
}

public class DeleteButton extends Button
{
    public function DeleteButton():void
    {
        _paddingX = 5;
        _paddingY = 2;

        super( 'x' );
    }
}

Now, I thought the altered _paddingX and _paddingY values in the inherited class would bubble up to the super class. But they don't. The DeleteButton is still constructed with the default values of the super class. I can't figure out how to do it anymore. What am I missing here?

Thanks in advance.

Edit:

I guess I had AS3's implementation mixed up with PHP's. This 'equivalent' example in PHP is perfectly legal:

class Button
{

    protected $_paddingX = 10;
    protected $_paddingY = 5;

    public function __construct( $label = '' )
    {
        var_dump( $label . ':' );
        var_dump( $this->_paddingX );
        var_dump( $this->_paddingY );
        echo '<br>';
    }
}

class DeleteButton extends Button
{
    public function __construct()
    {
        $this->_paddingX = 5;
        $this->_paddingY = 2;

        parent::__construct( 'x' );
    }
}

$button = new Button( 'browse' );
$deleteButton = new DeleteButton();

// this outputs:
string(7) "browse:" int(10) int(5) 
string(2) "x:" int(5) int(2)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

沧桑㈠ 2024-08-22 02:09:44

当您调用 super() 函数时,您还初始化了所有超类变量。因此,在您的示例中,当您调用 ..

_paddingX = 5;
_paddingY = 2;

.. 在调用 super() 之前,您实际上是在该函数内创建任意值。所以你说得对,只是这些变量在调用 super() 函数之前不存在。

public class Button
{
    private var _labelText:String;

    protected var _paddingX:Number;
    protected var _paddingY:Number;

    public function Button( labelText:String, paddingX:Number=10, paddingY:Number=5 ):void
    {
        _paddingX = paddingX;
        _paddingY = paddingY;

        _labelText = labelText;
    }
    public function traceVars():void {
        trace(_labelText);
        trace(_paddingX);
        trace(_paddingY);
    }
}

public class DeleteButton extends Button
{
    public function DeleteButton():void
    {
        super( 'x', 5, 2 );

    }
}

相反,您可以保留默认值(如 10 和 5),但可以根据需要更改它们。现在,如果您在主时间线上调用它,它应该适合您。

var but:DeleteButton = new DeleteButton();
but.traceVars();

希望这能解释它:)

乔治

when you call the super() function you also initialise all of the super classes variables. So in your example when you call..

_paddingX = 5;
_paddingY = 2;

..before calling super(), you are actually creating arbitrary values inside that function. So you're getting it right its just that those variables dont exist until the super() function has been called.

public class Button
{
    private var _labelText:String;

    protected var _paddingX:Number;
    protected var _paddingY:Number;

    public function Button( labelText:String, paddingX:Number=10, paddingY:Number=5 ):void
    {
        _paddingX = paddingX;
        _paddingY = paddingY;

        _labelText = labelText;
    }
    public function traceVars():void {
        trace(_labelText);
        trace(_paddingX);
        trace(_paddingY);
    }
}

public class DeleteButton extends Button
{
    public function DeleteButton():void
    {
        super( 'x', 5, 2 );

    }
}

Do this instead, this way you can keep the default values, as 10 and 5, but change them if needed. Now if you call this on the main timeline you it should work for you.

var but:DeleteButton = new DeleteButton();
but.traceVars();

Hope this explains it :)

George

傲娇萝莉攻 2024-08-22 02:09:44

我认为在设置 _paddingX 和 _paddingY 之前调用 super('x') 可能会有所不同:

 public function DeleteButton():void
    {
        super( 'x' );

        _paddingX = 5;
        _paddingY = 2;
    }

I think calling super('x') before setting the _paddingX and _paddingY could make a difference:

 public function DeleteButton():void
    {
        super( 'x' );

        _paddingX = 5;
        _paddingY = 2;
    }
情域 2024-08-22 02:09:44

您可以做的是重写 getter:

public class Button
{
    private var _labelText:String;

    protected var _paddingX:Number = 10;
    protected var _paddingY:Number = 5;

    public function Button( labelText:String ):void
    {
        _labelText = labelText;
    }

    protected function get paddingX():Number
    {
        return _paddingX;
    }
    // get paddingY ommited for brevity
}

public class DeleteButton extends Button
{
    public function DeleteButton():void
    {
        super( 'x' );
    }

    override protected function get paddingX():Number
    {
        return 42;
    }
}

父 Button 构造函数现在将使用子 DeleteButton 类中重写的 getter。

您还可以将 _paddingX/Y 设置为私有以将它们隐藏在实现中,并在 DeleteButton 中添加新的 _paddingX/Y。然而,这似乎是一种肮脏的做法,因为 DeleteButton 中的属性不是由 API 强制执行的。

What you could do is to override getters:

public class Button
{
    private var _labelText:String;

    protected var _paddingX:Number = 10;
    protected var _paddingY:Number = 5;

    public function Button( labelText:String ):void
    {
        _labelText = labelText;
    }

    protected function get paddingX():Number
    {
        return _paddingX;
    }
    // get paddingY ommited for brevity
}

public class DeleteButton extends Button
{
    public function DeleteButton():void
    {
        super( 'x' );
    }

    override protected function get paddingX():Number
    {
        return 42;
    }
}

The parent Button constructor will now use the overridden getter from child DeleteButton class.

You could also set _paddingX/Y to private to hide them from the implementation, and add new _paddingX/Y in DeleteButton. This however, seems like a dirty practice, because the properties in DeleteButton aren't enforced by API.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文