BlazeDS体系结构(四)

本文介绍了一种实现Java对象与ActionScript (AS) 对象互相转换的方法,通过定义特定的外部化接口来确保对象可以在两种不同的环境中正确传递。重点讨论了如何在Java和AS中实现IExternalizable接口以完成这一过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值