无法克隆扩展的 ByteArray [英] Trouble cloning an extended ByteArray

查看:27
本文介绍了无法克隆扩展的 ByteArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我扩展了 ByteArray 类,如下所示:

I've extended the ByteArray class, like this:

[RemoteClass(alias="MyByteArray")]
public class MyByteArray extends ByteArray {}

并使用 ByteArray#readObject()/writeObject() 克隆了此类的一个实例.但是,出于某种原因,克隆的对象是 ByteArray 的实例而不是 MyByteArray.下面的例子说明了这一点:

and cloned an instance of this class using ByteArray#readObject()/writeObject(). However, for some reason, the cloned object is an instance of ByteArray rather than MyByteArray. This is illustrated in the following example:

registerClassAlias("MyByteArray", MyByteArray);
var b1:MyByteArray = new MyByteArray();
var tmp:ByteArray = new ByteArray();
tmp.writeObject(b1);           
tmp.position = 0;
var b2:* = tmp.readObject();
trace( b2 is MyByteArray ); // prints false
trace( b2 is ByteArray ); // prints true

此外,当我在 MyByteArray 类中添加一些自定义字段时,它们不会与 writeObject() 一起保存,也不会被克隆...

Moreover, when I add some custom fields in MyByteArray class, they aren't saved with writeObject() and neither cloned...

谁能解释一下为什么克隆的对象不是 MyByteArray 的实例?

Can anybody explain me why the cloned object is not an instance of MyByteArray?

非常感谢!

推荐答案

我认为这是因为 tmp 是 ByteArray 类型,而不是 MyByteArray 类型.如果你制作 MyByteArray 类型的 tmp,它应该返回一个相同类型的对象,我认为使用 writeObject?或者您可能需要对其进行类型转换.

I think this is because tmp is of type ByteArray, not of type MyByteArray. If you make tmp of type MyByteArray, it should return an object of the same type, using writeObject i think? Or you may need to typecast it.

这篇关于无法克隆扩展的 ByteArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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