Java的Serializable序列化是個很好用的東西,平常可能很少會遇到使用。
Serializable大概來說就是把Java Object變成序列,可以傳輸到其它Java的應用程式上,最好的例子就是ServerSocket應用,大部份的二個不同的Service要相互利用socket溝通。
最簡單的方式就是傳送文字指令、xml、編碼過的資訊,而在Java裡提供了一個很好用的機制,當二邊Socket Service均使用Java開發時,則可在二邊放置相同的class Object(含有相同的package路徑),再利用java.io.ObjectInputStream及java.io.ObjectOutputStream來傳送及接到,可以設計一個Java Bean,在client接設定屬性後傳送給Server,在由Server接收後,直接取用Bean的值。
底下提供一個Java Bean implements Serializable的範列
public class User implements java.io.Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String account;
private String name;
private String isuse;
private String email;
private String mobile;
private String tel;
private String expain;
private String userType;
public User(String account, String name, String isuse, String email,
String mobile, String tel, String expain, String userType) {
super();
this.account = account;
this.name = name;
this.isuse = isuse;
this.email = email;
this.mobile = mobile;
this.tel = tel;
this.expain = expain;
this.userType = userType;
}
public User()
{
this("","","","","","","","");
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIsuse() {
return isuse;
}
public void setIsuse(String isuse) {
this.isuse = isuse;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getExpain() {
return expain;
}
public void setExpain(String expain) {
this.expain = expain;
}
public String getUserType() {
return userType;
}
public void setUserType(String userType) {
this.userType = userType;
}
}
One thought to “[Java]Serializable序列化”