如何正确扩展 AS3 Point 类?
我需要从我的观点中获得更多信息,所以我想我应该添加 previousX、previousY,以便我可以获得 deltaX 和 deltaY。现在,如果我创建一个简单的独立类,那么效果很好。
不过,我认为我想扩展 flash.geom.Point 类,以从它提供的其他函数和计算中受益。
所以我扩展了 Point 并根据需要修改了 x 和 y 设置器。当我编译时,我收到一个错误,指出这些 setter 没有标记为重写,而它们应该是。所以我重写了,但随后我收到一个错误,指出这些函数不会重写任何函数。
有什么线索我搞砸了吗?
这是课程:
package net.jansensan.geom
{
import flash.geom.Point;
/**
* @author Mat Janson Blanchet
*/
public class UpdatablePoint extends Point
{
private var _previousX : Number = 0;
private var _previousY : Number = 0;
private var _deltaX : Number = 0;
private var _deltaY : Number = 0;
// ********************************************************************
// [ PUBLIC METHODS ]
// ********************************************************************
public function UpdatablePoint(x:Number=0, y:Number=0)
{
super(x, y);
}
override public function toString():String
{
return "(x=" + super.x + ", y=" + super.y + ", previousX=" +
_previousX + ", previousY=" + _previousY + ", deltaX=" +
_deltaX + ", deltaY=" + _deltaY + ")";
}
// ********************************************************************
// [ GETTERS / SETTERS ]
// ********************************************************************
override public function set x(x:Number):void
{
_previousX = super.x;
super.x = x;
_deltaX = super.x - _previousX;
}
override public function set y(y:Number):void
{
_previousY = super.y;
super.y = y;
_deltaY = super.y - _previousY;
}
public function get previousX():Number
{
return _previousX;
}
public function get previousY():Number
{
return _previousY;
}
public function get deltaX():Number
{
return _deltaX;
}
public function get deltaY():Number
{
return _deltaY;
}
}
}
I come to need a bit more info from my points, so I thought I'd add a previousX, previousY so that I can get a deltaX and deltaY. Now, this works fine if I make a simple self-contained class.
I think, however, that I'd like to extend the flash.geom.Point class to benefit from the other functions and calculations it offers.
So I extended Point and modified the x and y setters as I needed. When I compile, I get an error that these setters are not marked for override when they should be. So I override, but then I get an error that says these do not override any function.
Any clue where I screwed up?
Here is the class:
package net.jansensan.geom
{
import flash.geom.Point;
/**
* @author Mat Janson Blanchet
*/
public class UpdatablePoint extends Point
{
private var _previousX : Number = 0;
private var _previousY : Number = 0;
private var _deltaX : Number = 0;
private var _deltaY : Number = 0;
// ********************************************************************
// [ PUBLIC METHODS ]
// ********************************************************************
public function UpdatablePoint(x:Number=0, y:Number=0)
{
super(x, y);
}
override public function toString():String
{
return "(x=" + super.x + ", y=" + super.y + ", previousX=" +
_previousX + ", previousY=" + _previousY + ", deltaX=" +
_deltaX + ", deltaY=" + _deltaY + ")";
}
// ********************************************************************
// [ GETTERS / SETTERS ]
// ********************************************************************
override public function set x(x:Number):void
{
_previousX = super.x;
super.x = x;
_deltaX = super.x - _previousX;
}
override public function set y(y:Number):void
{
_previousY = super.y;
super.y = y;
_deltaY = super.y - _previousY;
}
public function get previousX():Number
{
return _previousX;
}
public function get previousY():Number
{
return _previousY;
}
public function get deltaX():Number
{
return _deltaX;
}
public function get deltaY():Number
{
return _deltaY;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Point
类不是使用 get/set-methods 实现的,而是仅包含公共变量:public var x : Number
和public var y : Number< /代码>。请参阅此处的文档。
由于您无法覆盖任何公共 getter 或 setter,因此您无法检测到使用您的类的任何人何时写入这些变量。您可以做的是删除
extends Point
并向 UpdatablePoint 添加一个Point
实例变量。然后将 x/y 的 getter 和 setter 从 super.[x/y] 更改为 myPointInstance.[x/y]。如果您需要从 Point 类公开更多功能,您可以轻松地包装它。也就是说,假设您想使用 Point 中的“equals”方法,只需创建该方法签名的副本并使其主体包含:
return myPointInstance.equals(p);
The
Point
class isn't implemented using get/set-methods, rather it contains only public variables:public var x : Number
andpublic var y : Number
. See the documentation here.Since you cannot override any public getters or setters you can't detect when anyone using your class is writing to these variables. What you can do is remove the
extends Point
and add aPoint
instance variable to your UpdatablePoint. Then change your getters and setters for x/y from super.[x/y] to myPointInstance.[x/y].If you need to expose more functionality from the Point class, you could easily just wrap it. I.e. Say you want to use the 'equals'-method from Point, just create a copy of that method signature and make it's body contain:
return myPointInstance.equals(p);
Point 没有
公共函数集 x
或公共函数集 y
。所以你不能覆盖它们,因为它们不存在。观点非常低级,我的猜测是Adobe没有添加这些功能以避免过载的增加。
Point does not have
public function set x
orpublic function set y
.So you can't override them as they don't exist. Point is very low-level, my guess would be that Adobe didn't add these functions to avoid the increase in overload.