简体   繁体   English

实现保存和恢复对象属性状态的最佳方法

[英]Best way to implement save and restore object attribute state

What is the best way to implement save and restore / push and pop object attribute state? 实现保存和恢复/推送和弹出对象属性状态的最佳方法是什么? Like glPushMatrix() and glPopMatrix()? 像glPushMatrix()和glPopMatrix()?

For example: 例如:

obj.v1 = false;
obj.v2 = 0;
obj.v3 = "init";

obj.save();
obj.v1 = true;
obj.v2 = 2;
obj.v3 = "foo"
/* Do something... */

obj.restore();
/* Now v1 = false, v2 = 0, v3 = "init" again */

To save to a file: Make your class serializable 要保存到文件:使您的类可序列化

package com.yourpackage.name;
import java.io.Serializable;

public class myClass implements Serializable {
    public enum counterType {
        count_down,
        count_up
    }
    public int myIntValue= 0;
}

If you get the warning 'The Serializable class X does not declare a static final serialVersionUID field of type long', you can solve this by adding a value for serialVersionUID, eg: 如果您收到警告'可序列化类X未声明类型为long的静态最终serialVersionUID字段',您可以通过为serialVersionUID添加值来解决此问题,例如:

private static final long serialVersionUID = 1234567890abcdefg;

Declare your variable as you always do (eg in your main activity): 像往常一样声明你的变量(例如在你的主要活动中):

myClass myClassVariable = new myClass();
myClassVariable.myIntValue = 99;

Now you can save the data to a file, using: 现在,您可以使用以下方法将数据保存到文件中:

try
{
   FileOutputStream myFileOutputStream = new FileOutputStream(getFilesDir() + "file_name.ser");
   ObjectOutputStream myObjectOutputStream = new ObjectOutputStream(myFileOutputStream);
   myObjectOutputStream.writeObject(myClassVariable);
   myObjectOutputStream.close();
}
catch (Exception e)
{
    Log.e("Error when saving to file.",Log.getStackTraceString(e)); 
}

To load your data again: 要再次加载数据:

try
{
    FileInputStream myFileInputStream = new FileInputStream(getFilesDir() + "file_name.ser");
    ObjectInputStream myObjectInputStream = new ObjectInputStream(myFileInputStream);
    readClassVariable = (myClass) myObjectInputStream.readObject(); 
    myObjectInputStream.close();
}
catch (Exception e)
{
    Log.e("Error when loading from file.",Log.getStackTraceString(e));
}

readClassVariable now contains your saved class, eg readClassVariable.myIntValue is equal to 99 readClassVariable现在包含你保存的类,例如readClassVariable.myIntValue等于99

Hope it helps. 希望能帮助到你。 Comments welcome :) 欢迎评论:)

PS I need the rep, so don't forget to accept answer if you like it! PS我需要代表,所以如果你喜欢,不要忘记接受答案!

To save to RAM: 要保存到RAM:

You can simply copy it to another class variable. 您只需将其复制到另一个类变量即可。

However note that when you use the equal sign only the REFERENCE is copied (you are still working with the same variable): 但请注意,当您使用等号时,仅复制REFERENCE(您仍在使用相同的变量):

myClass1 = myClass2

In this case, the easiest is to make a new class and copy the parameters individually for all parameters: 在这种情况下,最简单的方法是创建一个新类并为所有参数单独复制参数:

myClass1.myIntValue = myClass2.myIntValue

This way you copy the value and not a reference to the (same) variable. 这样就可以复制而不是对(相同)变量的引用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM