Serializable接口的作用?(我是初学者)

viklove 2003-11-10 08:33:03
Serializable接口到底起什么样的作用,我不清楚,这几天看struts的源码,遇到这个问题,一直理解不好,请高手发表一下高见!谢谢!
...全文
2699 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
haoqingshi 2003-11-11
  • 打赏
  • 举报
回复
java很厉害的一个地方,比如你要传输一个对象给对方,但是你不可能将对象传输过去,所以你就要将对象序列化,将它转换为流的形式,然后通过管道传输过去,而接受的一方则用jvm将其再转换为对象
peter 2003-11-11
  • 打赏
  • 举报
回复
最简单的用法, "保存"的时候用! 可以保存对象的所有信息.(所有的对象都必须实现Serializable)
viklove 2003-11-11
  • 打赏
  • 举报
回复
谢谢,各位!
但是我还是不太明白,我想哪位高手给几个例子来说明一下
谢谢!
SmileAndHappy 2003-11-11
  • 打赏
  • 举报
回复
给一个在用Serializable接口时要注意的问题:
引用自api
Serialization
It is important to note that only AWT listeners which conform to the Serializable protocol will be saved when the object is stored. If an AWT object has listeners that aren't marked serializable, they will be dropped at writeObject time. Developers will need, as always, to consider the implications of making an object serializable. One situation to watch out for is this:
import java.awt.*;
import java.awt.event.*;
import java.io.Serializable;

class MyApp implements ActionListener, Serializable
{
BigObjectThatShouldNotBeSerializedWithAButton bigOne;
Button aButton = new Button();

MyApp()
{
// Oops, now aButton has a listener with a reference
// to bigOne!
aButton.addActionListener(this);
}

public void actionPerformed(ActionEvent e)
{
System.out.println("Hello There");
}
}
In this example, serializing aButton by itself will cause MyApp and everything it refers to to be serialized as well. The problem is that the listener is serializable by coincidence, not by design. To separate the decisions about MyApp and the ActionListener being serializable one can use a nested class, as in the following example:
import java.awt.*;
import java.awt.event.*;
import java.io.Serializable;

class MyApp java.io.Serializable
{
BigObjectThatShouldNotBeSerializedWithAButton bigOne;
Button aButton = new Button();

class MyActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println("Hello There");
}
}

MyApp()
{
aButton.addActionListener(new MyActionListener());
}
}
raymond323 2003-11-11
  • 打赏
  • 举报
回复
up
viklove 2003-11-11
  • 打赏
  • 举报
回复
此问题已经基本解决!

谢谢各位!
cowboy_ls 2003-11-11
  • 打赏
  • 举报
回复
我也是学JAVA不久
我的感觉:
JAVA里对象传的是引用;而不是对象的副本
所以在远程调用的时候会出问题(在C端的内存里找不倒S端的里对象的引用)
还有就是在BEAN里用的比较多点(需要象VB一样把组件的属性保存下来)
建议你多看看书
书是最好的老师
NightRanger 2003-11-10
  • 打赏
  • 举报
回复
JAVA最酷的特性,可以把本地的某个对象“压扁”,然后发到一个流对象中,(如一个文件,或一个网络端口),然后在另一个JVM中把它“膨胀”为原来的对象,因为JAVA的平台无关性,使得对象可以在很广的范围内传递。
yslfwww 2003-11-10
  • 打赏
  • 举报
回复
序列化接口,
web传输时使用,
只需要声明,没有要实现的方法。
运行时自动串行化

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧