[英]flex3 type casting
Does anyone know the real difference between the two ways of type casting in Flex 3? 有谁知道Flex 3中两种类型转换方式之间的真正区别?
var myObject1:MyObject = variable as MyObject;
var myObject2:MyObject = MyObject(variable);
I prefer to use the second method because it will throw an Error when type cast fails, whereas the first method will just return null. 我更喜欢使用第二种方法,因为它会在类型转换失败时抛出Error,而第一种方法只返回null。 But are there any other differences? 但是还有其他差异吗? Perhaps any advantages to using the first method? 使用第一种方法可能有什么好处?
The second type of casting has different behaviour for top level( http://livedocs.adobe.com/flex/2/langref/ ) types, eg Array(obj) does not cast in the straightforward way you describe; 第二种类型的转换对于顶级( http://livedocs.adobe.com/flex/2/langref/ )类型具有不同的行为,例如,Array(obj)不会以您描述的直接方式进行转换; it creates a new Array if possible from obj, even if obj is an Array. 如果可能的话,它会从obj创建一个新的数组,即使obj是一个数组。
I'm sure the times this would cause unexpected behaviour would be rare but I always use "as" for this reason. 我确信这会导致意外行为的时间很少,但我总是因此而使用“as”。 It means if I do 这意味着如果我这样做
int(str)
I know it's a cast in the "attempt to convert" sense of the word not in the "I promise it is" sense. 我知道这是“尝试转换”这个词的意义而不是“我保证是这样”的意义。
ref: got some confirmation on this from http://raghuonflex.wordpress.com/2007/07/27/casting-vs-the-as-operator/ 参考:从http://raghuonflex.wordpress.com/2007/07/27/casting-vs-the-as-operator/获得了一些确认
as
method returns null
if cast fails. 如果强制转换失败,则as
方法返回null
。 ()
method throws and error if the cast fails. 如果转换失败,则抛出()
方法并抛出错误。 If the value of variable
is not compatible with MyObject
, myObject1
will contain null
and you will be surprised by a null
pointer error ( 1009 : cannot access a property or method of a null object reference. ) somewhere later in the program when you try to access it. 如果variable
的值与MyObject
不兼容, myObject1
将包含null
,你会惊讶于null
指针错误( 1009:无法访问空对象引用的属性或方法。 )当你尝试以后在程序的某个地方访问它。 Where as if you are casting using the MyObject(variable)
syntax, you will get a type coercion error ( 1034 : Type Coercion failed: cannot convert _ to _ ) at the same line itself - which is more helpful than getting a 1009 somewhere later and wondering what went wrong. 如果您使用MyObject(variable)
语法进行转换,您将在同一行本身获得类型强制错误( 1034:类型强制失败:无法将_转换为_ ) - 这比以后在某处获得1009更有帮助并想知道出了什么问题。
I think I read somewhere on this site that as
is slighty faster than ()
, but I can't find the question again. 我觉得我在这个网站上阅读的地方as
是略低快于()
但我不能再次找到的问题。
Beside that this question have been asked many times, you will find an more in-depth answer here . 除了已经多次询问这个问题之外,你会在这里找到一个更深入的答案。
I recently discovered the very useful []
tag when searching on StackOverflow, it allows to only search in questions tagged with the specified tag(s). 我最近在StackOverflow上搜索时发现了非常有用的[]
标签,它只允许搜索用指定标签标记的问题。 So you could do a search like [actionscript-3] as vs cast . 所以你可以像[castcript-3]一样进行搜索。 There are more search tips here: https://stackoverflow.com/search . 这里有更多搜索提示: https : //stackoverflow.com/search 。
And no; 和不; the irony in that I can not find the question about performance and write about how to search is not lost on me ;) 具有讽刺意味的是,我无法找到关于性能的问题,并写下如何搜索不会丢失在我身上;)
我认为当返回基类时,并且当转换失败并且()抛出错误时不返回null。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.