在 Java 中序列化静态属性 [英] Serialize static attributes in Java

查看:52
本文介绍了在 Java 中序列化静态属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我尝试序列化一个静态属性会发生什么?

What happens if i'll try to serialize an attribute which is static?

谢谢

推荐答案

来自这篇文章:

提示 1:处理静态变量

Java 类通常包含一些静态中的全局相关值类变量.我们不会进入辩论的悠久历史全局变量的属性 -我们只想说程序员继续发现它们有用并且纯粹主义者建议的替代方案并不总是实用.

Tip 1: Handling Static Variables

Java classes often hold some globally relevant value in a static class variable. We won't enter into the long history of the debate over the propriety of global variables - let's just say that programmers continue to find them useful and the alternatives suggested by purists aren't always practical.

对于静态变量声明时初始化,序列化不存在任何特殊问题.第一次类被使用,变量在问题将被设置为正确价值.

For static variables that are initialized when declared, serialization doesn't present any special problems. The first time the class is used, the variable in question will be set to the correct value.

有些静态无法初始化这个办法.例如,它们可以由一个人在运行期间程序.假设我们有一个静态打开调试的变量类中的输出.这个变量可以通过发送一些在服务器上设置消息,可能来自监视器程序.我们也会想象当服务器收到此消息,操作员希望打开调试该类的所有后续使用连接到它的客户端服务器.

Some statics can't be initialized this way. They may, for instance, be set by a human during the running time of the program. Let's say we have a static variable that turns on debugging output in a class. This variable can be set on a server by sending it some message, perhaps from a monitor program. We'll also imagine that when the server gets this message, the operator wants debugging turned on in all subsequent uses of the class in the clients that are connected to that server.

程序员现在面临着困难.当有问题的班级到达客户端,静态变量的值并没有随之而来.但是,它包含默认静态时设置的类的无参数构造函数是由 writeObject() 调用.怎么可能客户端程序接收新的正确的值?

The programmer is now faced with a difficulty. When the class in question arrives at the client, the static variable's value doesn't come with it. However, it contains the default static state that's set when the class's no-argument constructor is called by writeObject(). How can the client programs receive the new correct value?

程序员可以创建另一个消息类型并将其传输到客户;然而,这需要一个消息类型的扩散,破坏使用的简单性序列化可以在消息传递.我们提出的解决方案with 是针对需要静态传输包括一个静态传输器"内部类.这类知道所有静态其外部类中的变量必须被设置.它包含一个成员变量对于每个必须是的静态变量连载.静态传输器副本静态到其成员变量在 writeObject() 方法中班级.readObject() 方法解开"这个包并传输服务器的静态设置客户端的变量.由于它是一个内部类,它将能够写入外部类的静态变量,无论隐私级别如何他们被声明.

The programmer could create another message type and transmit that to the client; however, this requires a proliferation of message types, marring the simplicity that the use of serialization can achieve in messaging. The solution we've come up with is for the class that needs the static transmitted to include a "static transporter" inner class. This class knows about all the static variables in its outer class that must be set. It contains a member variable for each static variable that must be serialized. StaticTransporter copies the statics into its member variables in the writeObject() method of the class. The readObject() method "unwraps" this bundle and transmits the server's settings for the static variables to the client. Since it's an inner class, it'll be able to write to the outer class's static variables, regardless of the level of privacy with which they were declared.

来自另一篇文章:

然而,这种轻松"在所有情况.正如我们将看到,序列化不是那么容易应用到具有静态或瞬态的类数据成员.仅关联数据具有类的特定实例是序列化,因此是静态数据,也就是说,与类相关联的数据与实例相反,不是自动序列化.序列化数据存储在静态变量一中必须提供特定于类的序列化.

Static or transient data

However, this "ease" is not true in all cases. As we shall see, serialization is not so easily applied to classes with static or transient data members. Only data associated with a specific instance of a class is serialized, therefore static data, that is, data associated with a class as opposed to an instance, is not serialized automatically. To serialize data stored in a static variable one must provide class-specific serialization.

同样,有些类可能定义用作临时的数据成员变量.序列化这些数据成员可能是不必要的.一些瞬态数据的例子包括运行时统计信息或哈希表映射参考.这些数据应该用瞬态修饰符标记以避免序列化.瞬态,由定义,用于指定数据程序员没有的成员想要或需要被序列化.看Java 简而言之,第 174 页:鼠标位置、首选大小、文件句柄(特定于机器的(本机代码)).

Similarly, some classes may define data members to use as scratch variables. Serializing these data members may be unnecessary. Some examples of transient data include runtime statistics or hash table mapping references. These data should be marked with the transient modifier to avoid serialization. Transient, by definition, is used to designate data members that the programmer does not want or need to be serialized. See Java in a Nutshell, page 174: mouse position, preferred size, file handles (machine specific (native code)).

写代码时声明为瞬态,然后触发(对程序员)的必要性特殊代码的可能性稍后连载.

When writing code if something is declared transient, then this triggers (to programmer) necessity of the posibility of special code for serialization later.

要序列化一个对象,您需要创建某种 OutputStream 对象和然后将它包裹在一个ObjectOutputStream 对象.在这点你只需要打电话writeObject() 并且你的对象是神奇地序列化并发送到输出流.为了扭转这个过程,你将 InputStream 包裹在一个ObjectInputStream 和调用readObject().回来的是,作为通常,向上转换对象的句柄,所以你必须垂头丧气来设置东西直的.如果你需要动态查询对象的类型,可以使用 getClass 方法.具体来说dk.getClass.getName() 返回名称dk 是一个实例的类的的.即,这向对象询问其对应类的名称目的.(嗯,是的,但是呢?句法?我仍然需要知道它是什么是声明它......太糟糕了)(C++可以在一个操作中完成此操作 (dynamic_cast(如果类型错误,则返回 null)),java 可以使用 instanceof 运算符来检查它是否是我的想法(见 Core Java, Ch5继承,铸造部分)

To serialize an object, you create some sort of OutputStream object and then wrap it inside an ObjectOutputStream object. At this point you only need to call writeObject() and your object is magically serialized and sent to the OutputStream. To reverse the process, you wrap an InputStream inside an ObjectInputStream and call readObject(). What comes back is, as usual, a handle to an upcast Object, so you must downcast to set things straight. If you need to dynamically query the type of the object, you can use the getClass method. Specifically dk.getClass.getName() returns the name of the class that dk is an instance of. I.e., this asks the object for the name of its corresponding class object. (Hmmm, True, but what about syntax? I still need to know what it is to declare it...too bad) (C++ can do this in one operation (dynamic_cast (gives null if wrong type)), java can use instanceof operator to check if it is what I think (see Core Java, Ch5 Inheritence, Casting section)

这篇关于在 Java 中序列化静态属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆