有没有办法对字符串、数字、uint、int 或其他“最终”进行伪子类化? Actionscript 3 中使用 Proxy 类的原语?
似乎有办法,但我没有看到。 过去,我曾在 Object 上使用 valueOf() 和 toString() 方法来使自定义对象根据上下文以数字或字符串的形式表现,但我想做更多。
It seems like there might be a way, but I'm not seeing it. I have, in the past, used the valueOf() and toString() methods on Object to cause custom objects to behave in numbers or strings based on context, but I'd like to do more.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基本上没有。 最终决定是最终决定,因此不能延长。 您可以创建一个与 Number 类具有所有相同方法的类,但就编译器而言,它仍然不会成为 Number。
老实说,永远不应该有理由需要从这些类进行扩展。
就代理而言,您可以考虑创建一个返回预先格式化的字符串/数字的工厂类,例如:
Basically no. Final is final so they cannot be extended. You could make a class which has all the same methods as the Number class, but it still wouldn't BE a Number as far as the compiler is concerned.
To be honest there should never be a reason that you should need to extend from these classes.
As far as proxies go you could consider making a factory class which returns a pre-formatted string/number eg:
正如 groady 已经指出的那样,这是不可能的...也不在您描述的场景中...但问题是,在运行时,类型检测机制非常简单...查找特征对象,并检查是否它匹配类/子类,或者它是否显式实现和接口...在任何其他情况下,您都会遇到错误...您可以使用代理来实现您自己的数组访问...但是,它们不会是数组,因此将它们传递给需要数组的函数,会导致错误......而且,在 AS3 中你不能重载运算符,所以你真的会遇到困难......你可以为数值创建一个类,但然后操作它会需要加法、减法等方法...但是,有一个 jira 上的相关请求< /a> ...不过,这并不能完全解决您的问题,因为您无法控制对象响应运算符的方式...如果您在 ECMA 兼容模式下编译,您可能将能够绕过严格的运行时类型检查,但另一方面,你会失去很多速度...最好的事情可能确实是创建一个必须通过方法而不是运算符来操作的类...不太舒服,但最好的 AS3 提供.. .
问候
back2dos
as already stated by groady, this is not possible ... also not in the scenarios you described ... but the thing is, that at runtime, the type detection mechanism is pretty easy ... lookup the traits object, and check whether it matches a class/subclass, or whether it explicitely implements and interface ... in any other case, you will have errors ... you can use proxies to implement your own array access ... however, they will not be arrays, thus passing them to a function that expects Array, will cause errors ... also, in AS3 you cannot overload operators, so you will really have a hard time ... you could create a class for numeric values, but then manipulating it would require methods as add, subtract etc. ... there is however a related request on jira ... still, this will not solve your problem entirely, because you cannot control the way an object responds to operators ... if you compile in ECMA compatibility mode, you probable will be able to bypass the strict runtime type checks, but on the other hand, you will lose a lot of speed ... the best thing probably really is creating a class that has to be manipulated through methods instead of operators ... not too comfortable, but the best AS3 offers ...
greetz
back2dos