问题描述
var n:Number = 1;
trace("n is Number:" + (n is Number)); //true
trace("n is int:" + (n is int)); //true
trace("n is uint:" + (n is uint)); //true
var m:Number = 1;
trace("m is Number:" + (m is Number)); //true
trace("m is int:" + (m is int)); //true
trace("m is uint:" + (m is uint)); //true
他们都成真!在ActionScript中,如何分辨是否一个数的类型,如果数量或者int或uint?
They all true! in actionscript, how to tell whether the type of a number if Number or int or uint?
推荐答案
在这里混淆源于有关如何AS3处理种类和数量奇特精妙之处。该是
运营商测试类的继承,但 INT
和 UINT
实际上不是类。 (这就是为什么他们没有得到资本 - 因为他们没有类的定义。)它们更像是类型关联,而如果使用得当能获得您的某些便利和性能方面的改进。但是,对于继承的目的,一个号是一个号是一个号码。
The confusion here stems from a peculiar subtlety about how AS3 handles types and numbers. The is
operator tests class inheritance, but int
and uint
are not actually classes. (That's why they don't get capitalized - because they have no class definition.) They're more like type associations, which if used properly can gain you certain conveniences and performance improvements. But for inheritance purposes, a Number is a Number is a Number.
这意味着在实践中,如果你作出这样的类型是,比方说, UINT
则Flash将在内部的32位无符号格式存储值的变量(而不是用于64位格式数
)。如果你改变变量的值,它会留在相同的格式,等等 UINT
的限制将被强制执行:
What this means in practice is, if you make a variable that is typed as, say, uint
then Flash will store that value internally in a 32-bit unsigned format (rather than the 64bit format used for Number
). And if you change the value of that variable, it will remain in the same format, so the restrictions on uint
will be enforced:
var a:uint = 0;
a--;
trace(a); // 4294967295 - it wrapped around
但它确实在引用的到你的电话号码,它的类型为 UINT
,而不是数字本身。如果你犯了一个新的类型化的参考,这将是显而易见的:
But it's really the reference to your number that is typed as uint
, not the number itself. If you make a new untyped reference, this will be apparent:
var a:uint = 0;
var b:* = a;
b--
trace(b); // -1
所以回到你的问题,你应该如何实现您的缓冲区作家?由于在Flash中如何处理这些类型的,我不认为有一个绝对正确的答案固有的精妙之处。一种方法是使用 UINT
或 INT
如果数据符合这些类型的限制,并使用数
否则。这将节省内存,它可能preserve准确性。但是,对待所有的数字为数
也让我觉得作为一个防御性的做法。我认为这取决于你打算做什么用的缓冲区。
So to return to your problem, how should you implement your buffer writer? Due to the inherent subtlety in how Flash treats these types I don't think there's an absolutely correct answer. One approach would be to use uint
or int
if the data meets the restrictions on those types, and use Number
otherwise. This would save memory and it could preserve accuracy. But treating all numbers as Number
also strikes me as a defensible approach. I think it depends on what you plan to do with the buffer.
这篇关于在ActionScript中,如何分辨是否一个数的类型,如果数量或者int或uint?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!