Java对象转换为AS对象
[img]http://dl.iteye.com/upload/picture/pic/75509/41d78d52-d456-3345-8101-ddd67f56c35d.jpeg[/img]
[img]http://dl.iteye.com/upload/picture/pic/75507/1c2f3ccc-57ba-3384-91e2-5037c2e07f13.jpeg[/img]
[img]http://dl.iteye.com/upload/picture/pic/75505/839cfd40-86dc-3f24-945d-f71892fa4a73.jpg[/img]
// Product.as
package samples.externalizable {
import flash.utils.IExternalizable;
import flash.utils.IDataInput;
import flash.utils.IDataOutput;
[RemoteClass(alias="samples.externalizable.Product")]
public class Product implements IExternalizable {
public function Product(name:String=null) {
this.name = name;
}
public var id:int;
public var name:String;
public var properties:Object;
public var price:Number;
public function readExternal(input:IDataInput):void {
name = input.readObject() as String;
properties = input.readObject();
price = input.readFloat();
}
public function writeExternal(output:IDataOutput):void {
output.writeObject(name);
output.writeObject(properties);
output.writeFloat(price);
}
}
}
// Product.java
package samples.externalizable;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Map;
public class Product implements Externalizable {
private String inventoryId;
public String name;
public Map properties;
public float price;
public Product(){
}
public String getInventoryId() {
return inventoryId;
}
public void setInventoryId(String inventoryId) {
if (inventoryId != null && inventoryId.startsWith("X")){
this.inventoryId = inventoryId;
}else{
throw new IllegalArgumentException("3rd party product
inventory identities must start with 'X'");
}
}
public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
name = (String)in.readObject();
properties = (Map)in.readObject();
price = in.readFloat();
setInventoryId(lookupInventoryId(name, price));
}
public void writeExternal(ObjectOutput out) throws IOException {
// Write out the client properties from the server representation
out.writeObject(name);
out.writeObject(properties);
out.writeFloat(price);
}
private static String lookupInventoryId(String name, float price) {
String inventoryId = "X" + name + Math.rint(price);
return inventoryId;
}
}