Actionscript 3 as3:如何按值复制对象
我需要在每个其他对象中有一个公共对象的实例,我有。我正在修改我拥有的每个子对象中该对象的值 比如说。我有一张瓷砖地图,还有一个机器人按特定顺序在上面移动。每个机器人都在将他已经访问过的瓷砖标记为visited=true。但总的来说,我不想改变主地图 我试图树立一个榜样:Actionscript 3 as3:如何按值复制对象,actionscript-3,flex3,Actionscript 3,Flex3,我需要在每个其他对象中有一个公共对象的实例,我有。我正在修改我拥有的每个子对象中该对象的值 比如说。我有一张瓷砖地图,还有一个机器人按特定顺序在上面移动。每个机器人都在将他已经访问过的瓷砖标记为visited=true。但总的来说,我不想改变主地图 我试图树立一个榜样: <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
applicationComplete="complete()">
<mx:Script>
<![CDATA[
private var array:Array = new Array( 1, 2, 3, 4);
public function complete():void
{
trace("here " + array);
var a:Array = array;
a[0] = 100;
trace("here " + array);
}
]]>
</mx:Script>
</mx:Application>
有人能帮助我理解如何复制数组(例如,按值复制,而不是按引用复制)吗
编辑:
可能包含一些语法错误
public class MyObject
{
private var arr:Array;
private var bool:Boolean;
// ...
public function clone ():MyObject
{
var obj:MyObject = new MyObject();
// clone values
obj.arr = this.arr.slice();
obj.bool = this.bool;
return obj;
}
}
对于克隆阵列,您可以使用
以下是poke所述方法的替代方法: 首先,我想谈一谈波克的帖子
var t:Array = [];
t[0] = [1, 2, 3];
t[1] = new Dictionary();
t[1]['hello'] = 'world';
t[2] = {'my': 'object'}
trace(t, t[1]['hello'], t[2]['my']); // [trace] 1,2,3,[object Dictionary],[object Object] world object
var t2:Array = clone(t);
trace(t2, t2[1]['hello'], t2[2]['my']); // [trace] 1,2,3,[object Dictionary],[object Object] world object
t[0] = [4, 5, 6];
t[1]['hello'] = 'earth';
t[2]['my'] = 'other object';
trace('modified values'); // [trace] modified values
trace(t, t[1]['hello'], t[2]['my']); // [trace] 4,5,6,[object Dictionary],[object Object] earth other object
trace(t2, t2[1]['hello'], t2[2]['my']); // [trace] 1,2,3,[object Dictionary],[object Object] world object
function clone(source:*):* {
var b:ByteArray = new ByteArray();
b.writeObject(source);
b.position = 0;
return(b.readObject());
}
这涵盖了自定义克隆方法的第一个问题和上面的第1点。如您所见,所有对象及其内容都是使用内置方法复制的
我在这里展示了如何创建clone方法的实现,但是您可以在:mx.utils.ObjectUtil中找到一个
如果您想深度复制一个私有存储其数据的对象,那么必须实现IExternalizable接口。这将迫使您实现两种方法:
public function writeExternal(output:IDataOutput):void
public function readExternal(input:IDataInput):void
在这些函数中,您将私有变量写入输出对象,然后将它们从输入读取到私有变量。然后,当您调用clone时,您将获得对象的完整副本。记住对所有嵌套对象执行此操作
下面是一个包含两个类的简单实现示例:
package {
import flash.utils.IExternalizable;
import flash.utils.IDataInput;
import flash.utils.IDataOutput;
import flash.net.registerClassAlias;
public class Car implements IExternalizable {
private var type:String;
private var contents:Array;
public function Car() {
registerClassAlias("Car", Car);
}
public function setVars(type:String, contents:Array):void {
this.type = type;
this.contents = contents;
}
public function setType(type:String):void {
this.type = type;
}
public function writeExternal(output:IDataOutput):void {
output.writeUTF(type);
output.writeObject(contents);
}
public function readExternal(input:IDataInput):void {
type = input.readUTF();
contents = input.readObject();
}
public function toString():String {
return "[Car type = " + type + ", contents = " + contents + "]";
}
}
}
以及:
要测试它们:
package {
import flash.display.Sprite;
import flash.utils.ByteArray;
import flash.utils.Dictionary;
public class Serial extends Sprite {
public function Serial() {
var person0:Person = new Person();
person0.setVars("John", "Doe");
var person1:Person = new Person();
person1.setVars("Jane", "Doe");
var car0:Car = new Car();
car0.setVars("Ford", [person0, person1]);
var person2:Person = new Person();
person2.setVars("Joe", "Bloggs");
var car1:Car = new Car();
car1.setVars("Vauxhall", [person2]);
var street:Array = [car0, car1];
trace("street = " + street); // [trace] street = [Car type = Ford, contents = [Person firstName = John, secondName = Doe],[Person firstName = Jane, secondName = Doe]],[Car type = Vauxhall, contents = [Person firstName = Joe, secondName = Bloggs]]
var street2:Array = clone(street);
trace("street2 = " + street2); // [trace] street2 = [Car type = Ford, contents = [Person firstName = John, secondName = Doe],[Person firstName = Jane, secondName = Doe]],[Car type = Vauxhall, contents = [Person firstName = Joe, secondName = Bloggs]]
person0.setVars("Max", "Headroom");
person1.setVars("Simon", "Le Bon");
car0.setType("Mini");
person2.setVars("Harry", "Wotsit");
car1.setType("Austin");
trace("modified values of street"); // [trace] modified values of street
trace("street = " + street); // [trace] street = [Car type = Mini, contents = [Person firstName = Max, secondName = Headroom],[Person firstName = Simon, secondName = Le Bon]],[Car type = Austin, contents = [Person firstName = Harry, secondName = Wotsit]]
trace("street2 = " + street2); // [trace] street2 = [Car type = Ford, contents = [Person firstName = John, secondName = Doe],[Person firstName = Jane, secondName = Doe]],[Car type = Vauxhall, contents = [Person firstName = Joe, secondName = Bloggs]]
}
private function clone(source:*):* {
var b:ByteArray = new ByteArray();
b.writeObject(source);
b.position = 0;
return(b.readObject());
}
}
}
这涵盖了自定义克隆方法的第二个问题。正如您所看到的,我们不必担心调用任何克隆方法,这一切都是为我们准备的
我不是说它不完全没有缺点,但它
package {
import flash.utils.IExternalizable;
import flash.utils.IDataInput;
import flash.utils.IDataOutput;
import flash.net.registerClassAlias;
public class Car implements IExternalizable {
private var type:String;
private var contents:Array;
public function Car() {
registerClassAlias("Car", Car);
}
public function setVars(type:String, contents:Array):void {
this.type = type;
this.contents = contents;
}
public function setType(type:String):void {
this.type = type;
}
public function writeExternal(output:IDataOutput):void {
output.writeUTF(type);
output.writeObject(contents);
}
public function readExternal(input:IDataInput):void {
type = input.readUTF();
contents = input.readObject();
}
public function toString():String {
return "[Car type = " + type + ", contents = " + contents + "]";
}
}
}
package {
import flash.utils.IExternalizable;
import flash.utils.IDataInput;
import flash.utils.IDataOutput;
import flash.net.registerClassAlias;
public class Person implements IExternalizable {
private var firstName:String;
private var secondName:String;
public function Person() {
registerClassAlias("Person", Person);
}
public function setVars(firstName:String, secondName:String):void {
this.firstName = firstName;
this.secondName = secondName;
}
public function writeExternal(output:IDataOutput):void {
output.writeUTF(firstName);
output.writeUTF(secondName);
}
public function readExternal(input:IDataInput):void {
firstName = input.readUTF();
secondName = input.readUTF();
}
public function toString():String {
return "[Person firstName = " + firstName + ", secondName = " + secondName + "]";
}
}
}
package {
import flash.display.Sprite;
import flash.utils.ByteArray;
import flash.utils.Dictionary;
public class Serial extends Sprite {
public function Serial() {
var person0:Person = new Person();
person0.setVars("John", "Doe");
var person1:Person = new Person();
person1.setVars("Jane", "Doe");
var car0:Car = new Car();
car0.setVars("Ford", [person0, person1]);
var person2:Person = new Person();
person2.setVars("Joe", "Bloggs");
var car1:Car = new Car();
car1.setVars("Vauxhall", [person2]);
var street:Array = [car0, car1];
trace("street = " + street); // [trace] street = [Car type = Ford, contents = [Person firstName = John, secondName = Doe],[Person firstName = Jane, secondName = Doe]],[Car type = Vauxhall, contents = [Person firstName = Joe, secondName = Bloggs]]
var street2:Array = clone(street);
trace("street2 = " + street2); // [trace] street2 = [Car type = Ford, contents = [Person firstName = John, secondName = Doe],[Person firstName = Jane, secondName = Doe]],[Car type = Vauxhall, contents = [Person firstName = Joe, secondName = Bloggs]]
person0.setVars("Max", "Headroom");
person1.setVars("Simon", "Le Bon");
car0.setType("Mini");
person2.setVars("Harry", "Wotsit");
car1.setType("Austin");
trace("modified values of street"); // [trace] modified values of street
trace("street = " + street); // [trace] street = [Car type = Mini, contents = [Person firstName = Max, secondName = Headroom],[Person firstName = Simon, secondName = Le Bon]],[Car type = Austin, contents = [Person firstName = Harry, secondName = Wotsit]]
trace("street2 = " + street2); // [trace] street2 = [Car type = Ford, contents = [Person firstName = John, secondName = Doe],[Person firstName = Jane, secondName = Doe]],[Car type = Vauxhall, contents = [Person firstName = Joe, secondName = Bloggs]]
}
private function clone(source:*):* {
var b:ByteArray = new ByteArray();
b.writeObject(source);
b.position = 0;
return(b.readObject());
}
}
}