AS3:如何使用&QUOT如"运营商? [英] AS3: How to use the "as" operator?

查看:124
本文介绍了AS3:如何使用&QUOT如"运营商?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var o:Object = {title: 'asad'};
var v:ImageItemVO = o as ImageItemVO;
var v:ImageItemVO = ImageItemVO(o); // throws an error

我ImageItemVO有一个名为标题公共变种。 在此之后code运行变种V为空。为什么? 有人可以给我如何使它工作的例子吗?

my ImageItemVO has a public var named title. After this code runs the "var v" is null. Why? Can somebody give me an example of how to make it work?

推荐答案

运算符用于铸造一个对象从一种类型到另一种,但如果只能对象可以被铸造的方式。如果不能,它会给你。铸造(你做的最后一行的方式)的另一种方式,反而给你一个错误,如果对象不能被铸造。

The as operator is used for casting an object from one type to another, but only works if the object can be casted that way. If it can't it will give you null. The other way of casting (the way you do it on the last line), will instead give you an error if the object cannot be casted.

在这种情况下,你不想投所有,铸造不这样的。相反,你可能想要做这样的事情:

In this case you don't want to cast at all, casting doesn't work that way. Instead, you probably want to do something like this:

var v: ImageItemVO = new ImageItemVO();
v.title = "asad";

如果有更多的属性,你不想用手把它们全部输入:

or if there are more properties, and you don't want to type them all by hand:

var o: Object = { ... };
var v: ImageItemVO = new ImageItemVO();
for (var key: String in o) {
  v[key] = o[key];
}

这code将0 复制的所有属性 v

这篇关于AS3:如何使用&QUOT如"运营商?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆