AS3:覆盖子类中的受保护变量
我这里停电了我以为我理解了这些原则,但我似乎无法再让它发挥作用了。我想让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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您调用 super() 函数时,您还初始化了所有超类变量。因此,在您的示例中,当您调用 ..
.. 在调用 super() 之前,您实际上是在该函数内创建任意值。所以你说得对,只是这些变量在调用 super() 函数之前不存在。
相反,您可以保留默认值(如 10 和 5),但可以根据需要更改它们。现在,如果您在主时间线上调用它,它应该适合您。
希望这能解释它:)
乔治
when you call the super() function you also initialise all of the super classes variables. So in your example when you call..
..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.
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.
Hope this explains it :)
George
我认为在设置 _paddingX 和 _paddingY 之前调用 super('x') 可能会有所不同:
I think calling super('x') before setting the _paddingX and _paddingY could make a difference:
您可以做的是重写 getter:
父 Button 构造函数现在将使用子 DeleteButton 类中重写的 getter。
您还可以将 _paddingX/Y 设置为私有以将它们隐藏在实现中,并在 DeleteButton 中添加新的 _paddingX/Y。然而,这似乎是一种肮脏的做法,因为 DeleteButton 中的属性不是由 API 强制执行的。
What you could do is to override getters:
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.