如果真如你所说的,A、B两个类的对象仅仅是保持指向同一个C类对象的引用而非包含,那么,A、B类中引用的永远是同一个C类对象。所以,你完全不用担心A类对象对C的对象会有何影响,只需要你保证将同一个C类对象的引用传给新生成的C类对象就可以了。
解决方案 »
- 一个Java试题,判断航班日期的,求解!
- java浏览器怎样支持css和JavaScript啊
- java 中多线程执行完后输出结果的问题
- String choice=showMenu(); 什么意思?
- 右移相当于除2,除不尽怎么办?
- 推荐几个学JAVA的学习网站
- Type safety: The method add(Object) belongs to the raw type List. References to generic type must be parameterized
- 关于image图片的读出问题,请大虾指教!
- 如何在进程Process pcommand= Runtime.getRuntime().exec(cmd);中创建线程?
- 在jbuilder环境下运行一切正常,在字符截面下就出现这种情况,为何
- 简单调查一下,请问大家写程序都是在多少岁开始学的啊?
- 用过Log4j的朋友请进来帮帮忙!!!!!!!!!!!
import java.io.*;
public class SerizableTest
{
public static void main( String args[] )
{
C temp = new C();
A a = new A(temp );
B b = new B(temp );
try{
ObjectOutputStream output = new ObjectOutputStream(
new FileOutputStream( "aFile" ) );
output.writeObject( a );
output.flush();
output.close();
}catch( IOException e ) { }
b.m_BC.data = "modify";
try{
ObjectOutputStream output = new ObjectOutputStream(
new FileOutputStream( "bFile" ) );
output.writeObject( b );
output.flush();
output.close();
}catch( IOException e ) { }
try{
ObjectInputStream input = new ObjectInputStream(
new FileInputStream( "aFile" ) );
a= (A) input.readObject();
}catch( Exception e ){}
System.out.println( "A: "+a.m_AC.data );
System.out.println( "B: "+b.m_BC.data );
}
}class A implements Serializable
{
C m_AC;
A( C ac )
{
m_AC = ac;
}
}class B implements Serializable
{
C m_BC;
B( C bc )
{
m_BC = bc;
}
}class C implements Serializable
{
String data;
C()
{
data = new String(" ini " );
}
}
class A{
... public void setC( C ac )
{
m_AC = ac;
} ...
}并在a= (A) input.readObject()后添加:
a.setC(temp);
你说的是一个办法,不过上面的例子只是一个十分简单的例子。而到了比较复杂的系统中,就不一定能判断是否temp已经改变。
而且,再次读入后也不一定能够找到temp(因为系统中的对象是很多的,而且会在不同的地方被修改)。
有没有什么其他的方法呢?
能不能让系统自己去完成这样的工作能?
{
private static C constant = null; private C //构造
{
} public synchronized static C getInstance()
{
if( constant == null )
constant = new C();
return constant;
}}这样,你在系统中多个类中对C类的引用做修改时,使用C.getInstance()即可。这样的设计模式就能达到你的目的。
the default serialization will not guarantee identity consistency across different serializations.so, if you have object graph like d refers to a and b, a refers to c, b refers to c.
writeObject(d) and d=readObject() will make sure you still get the exact object layout.But, if you call a=readObject(), b=readObject(), a and b may not be refering to the same c object.a possible solution would be:
override the writeObject and readObject, save the object id instead of the object itself when you are serializing c.
then, use a hashtable to store the mapping between the object id and the object.But, this does not always solve the problem. If you have a circular reference, it'll be very hard, if possible.So, perfect solution would be to try serializing the whole object graph in one serialization.
if you cannot do that, think about your design first.
那么在我不想把C类取消的情况下,该如何设计能?
谢谢关心。
use a Registration class to do that.
then,
class Acad implements Serializable{
private Student[] students;
private Course[] courses;
private Registration reg;
}
then serialize Acad.
2. with the Acad object, you don't need object id. the serialization will take care of the identity consistency for you when you do writeObject(acad);
原来的Registration类是一个关联类,
一个学生或课程可以与多个Registration关联,但每个Registration只能与一个学生和一个课程关联啊?那么Acad起到了什么作用呢?它里面有多个学生多个课程却只有一个Registration??
and then, writeObject can take care of the consistency.
if you call writeObject(students), writeObject(registration), writeObject(courses) individually,
it does not guarantee that.