AS3 保护被破坏了?
我有一个令人高兴的 Flash AS3 难题。给出以下代码:
public class BaseClass
{
protected var _A:uint;
}
public class ExtendedClass extends BaseClass
{
public static function readBaseA(a:BaseClass) : uint
{
return a._A;
}
}
所以你看,静态函数正在利用其读取 BaseClass 的受保护成员的能力,以返回 _A。
但这并不能计算,Flash 对我咆哮道:
(hidden)\ExtendedClass.as, Line 7 1178: Attempted access of inaccessible property _A through a reference with static type BaseClass.
在 Java 中,这没关系。我认为 Java 是基本 OO 的参考实现。
如果我从 readBaseA 方法中去掉“静态”,那么现在它是一个实例方法,我仍然无法从 BaseClass 实例中读取 _A 。但有趣的是,我可以从这里读到它。_A。
就像 AS3 区分 this 的受保护成员和其他对象的受保护成员一样,尽管这些对象可能是与“this”同一类的实例。
有人在野外见过类似的行为吗?
富有的
I have a joyful Flash AS3 conundrum. Given the following code:
public class BaseClass
{
protected var _A:uint;
}
public class ExtendedClass extends BaseClass
{
public static function readBaseA(a:BaseClass) : uint
{
return a._A;
}
}
So you see, the static function is using its ability to read the protected member of BaseClass, to return _A.
But this does not compute, and Flash barks back at me with:
(hidden)\ExtendedClass.as, Line 7 1178: Attempted access of inaccessible property _A through a reference with static type BaseClass.
In Java, this is okay. And I consider Java the reference implementation of basic OO.
if I take away the 'static' from that readBaseA method, so that now it is an instance method, I still cannot read the _A from a BaseClass instance. But interestingly, I can read it from this._A.
It's like AS3 is differentiating between this's protected members, and other objects' protected members, although those objects may be instances of the same class as 'this'.
Anyone seen similar behaviour out there in the wild?
Rich
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
_A
受到保护,因此只能从同一个类或其子类中访问它,并且由于它没有声明为静态“类”,因此在这方面实际上意味着“类实例”。澄清一下,实例
a
无法访问实例b
的私有或受保护属性,无论它们是否具有完全相同的类型或者是否扩展了另一个。_A
is protected so it can only be accessed from within the same class or it's sub-classes, and since it's not declared as static "class" in this regard actually means "class instance".To clarify, an instance
a
cannot access a private or protected property of instanceb
, regardless of whether they have the exact same type or if one extends the other.如果您使用
internal
而不是protected
实例属性,它就会起作用。 (如果这对您来说足够隐私的话)ActionScript 对于 Adobe 的
protected
的含义非常特殊:因此,它仅适用于专门的引用,不幸的是,在您的情况下,它们显然仅意味着实例引用。这可能与 ActionScript 所基于的基于原型的实现有关,其中它仅检查
prototype
对象而不是constructor
对象来确定是否允许访问受保护的成员。it'll work if you use
internal
instead ofprotected
instance property. (if that is enough privacy for you)ActionScript is very particular about what
protected
means, from Adobe:So its only available to references specifically, and unfortunately in your case, they obviously mean instance references only. This might have something to do with the prototype-based implementation that ActionScript is built on, where its only checking the
prototype
object and not theconstructor
object to determine if access is allowed protected member.这就是它在 AS3 中的工作方式:
可以从扩展类访问和覆盖
protected
字段,并且在任何其他情况下都将被视为private
that's how it works in AS3:
protected
fields can be accessed and overriden from extended classes and are treated asprivate
for any other case