AS3:如何使用&QUOT如"运营商?如何使用、运营商、QUOT

2023-09-08 15:26:51 作者:要不要O你的

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