有没有办法让currentclass指向刚读入Java的新对象Serializable?
Is there a way to make current class point to the new object that has just been read in Java Serializable?
我正在尝试读取用于社交网络模拟的对象。读取方法使用 Java Serializable。代码如下所示:
public class SocialNetwork implements Serializable{
// lots of fields
public SocialNetwork(){
//lots of inilization
}
public void writethisObject() throws IOException{
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("Simulation.bin"));
objectOutputStream.writeObject(this);
}
public void readfromObject(File f) throws IOException, ClassNotFoundException{
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(f));
SocialNetwork newSocialNetwork = (SocialNetwork) objectInputStream.readObject();
this = newSocialNetwork;
}
}
然而,如您所见,我试图通过使 this = newSocialNetwork.这给了我预期的错误。我可以通过将当前 SocialNetwork class 的每个字段设置为 newSocialNetwork 来解决这个问题。但是,我不想这样做,因为我的 class 中有大量字段。而且会显得很乱。我希望您已经了解我正在尝试做的事情。
你不能那样做,但你可以声明一个静态方法。
public static SocialNetwork readfromObject(File f) throws IOException, ClassNotFoundException{
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(f));
return (SocialNetwork) objectInputStream.readObject();
}
由于您无法在 Java 中覆盖它,并且您只想让 class 的一个实例使用 Singelton Pattern:
public class SocialNetwork implements Serializable{
// lots of fields
private static SocialNetwork myself = new SocialNetwork();
private SocialNetwork(){ // private constructor
//lots of inilization
}
public static SocialNetwork getInstance() {
return myself;
}
public void writethisObject() throws IOException{...}
public void readfromObject(File f) throws IOException, ClassNotFoundException{
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(f));
myself = (SocialNetwork) objectInputStream.readObject();
}
}
我们可以使用单例模式来实现。 类 外部社交网络将使用 SocialNetwork.getInstance() 来初始化社交网络。这是代码
public class SocialNetwork implements Serializable{
// lots of fields
private static SocialNetwork myself = null;
private SocialNetwork(){
//lots of inilization
}
public static SocialNetwork getInstance() {
if (myself == null){
myself = new SocialNetwork();
}
return myself;
}
public void writethisObject() throws IOException{
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("Simulation.bin"));
objectOutputStream.writeObject(this);
}
public void readfromObject(File f) throws IOException, ClassNotFoundException{
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(f));
SocialNetwork newSocialNetwork = (SocialNetwork) objectInputStream.readObject();
myself = newSocialNetwork;
}
}
我正在尝试读取用于社交网络模拟的对象。读取方法使用 Java Serializable。代码如下所示:
public class SocialNetwork implements Serializable{
// lots of fields
public SocialNetwork(){
//lots of inilization
}
public void writethisObject() throws IOException{
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("Simulation.bin"));
objectOutputStream.writeObject(this);
}
public void readfromObject(File f) throws IOException, ClassNotFoundException{
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(f));
SocialNetwork newSocialNetwork = (SocialNetwork) objectInputStream.readObject();
this = newSocialNetwork;
}
}
然而,如您所见,我试图通过使 this = newSocialNetwork.这给了我预期的错误。我可以通过将当前 SocialNetwork class 的每个字段设置为 newSocialNetwork 来解决这个问题。但是,我不想这样做,因为我的 class 中有大量字段。而且会显得很乱。我希望您已经了解我正在尝试做的事情。
你不能那样做,但你可以声明一个静态方法。
public static SocialNetwork readfromObject(File f) throws IOException, ClassNotFoundException{
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(f));
return (SocialNetwork) objectInputStream.readObject();
}
由于您无法在 Java 中覆盖它,并且您只想让 class 的一个实例使用 Singelton Pattern:
public class SocialNetwork implements Serializable{
// lots of fields
private static SocialNetwork myself = new SocialNetwork();
private SocialNetwork(){ // private constructor
//lots of inilization
}
public static SocialNetwork getInstance() {
return myself;
}
public void writethisObject() throws IOException{...}
public void readfromObject(File f) throws IOException, ClassNotFoundException{
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(f));
myself = (SocialNetwork) objectInputStream.readObject();
}
}
我们可以使用单例模式来实现。 类 外部社交网络将使用 SocialNetwork.getInstance() 来初始化社交网络。这是代码
public class SocialNetwork implements Serializable{
// lots of fields
private static SocialNetwork myself = null;
private SocialNetwork(){
//lots of inilization
}
public static SocialNetwork getInstance() {
if (myself == null){
myself = new SocialNetwork();
}
return myself;
}
public void writethisObject() throws IOException{
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("Simulation.bin"));
objectOutputStream.writeObject(this);
}
public void readfromObject(File f) throws IOException, ClassNotFoundException{
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(f));
SocialNetwork newSocialNetwork = (SocialNetwork) objectInputStream.readObject();
myself = newSocialNetwork;
}
}