首页 > 编程语言 >《Java编程思想第四版》学习笔记32--关于static字段的序列化

《Java编程思想第四版》学习笔记32--关于static字段的序列化

时间:2023-09-29 20:32:30浏览次数:36  
标签:dim Java -- yVal int static 序列化 public color

//: CADState.java
// Saving and restoring the state of a
// pretend CAD system.
import java.io.*;
import java.util.*;
abstract class Shape implements Serializable {
public static final int
RED = 1, BLUE = 2, GREEN = 3;
private int xPos, yPos, dimension;
private static Random r = new Random();
private static int counter = 0;
abstract public void setColor(int newColor);
abstract public int getColor();
public Shape(int xVal, int yVal, int dim) {
xPos = xVal;
yPos = yVal;
dimension = dim;
}
public String toString() {
return getClass().toString() +
" color[" + getColor() +
"] xPos[" + xPos +
"] yPos[" + yPos +
"] dim[" + dimension + "]\n";
}
public static Shape randomFactory() {
int xVal = r.nextInt() % 100;
int yVal = r.nextInt() % 100;
int dim = r.nextInt() % 100;
switch(counter++ % 3) {
default:
case 0: return new Circle(xVal, yVal, dim);
case 1: return new Square(xVal, yVal, dim);
case 2: return new Line(xVal, yVal, dim);
}
}
}
class Circle extends Shape {
private static int color = RED;
public Circle(int xVal, int yVal, int dim) {
super(xVal, yVal, dim);
}
public void setColor(int newColor) {
color = newColor;
}
public int getColor() {
return color;
}
}
class Square extends Shape {
private static int color;
public Square(int xVal, int yVal, int dim) {
super(xVal, yVal, dim);
color = RED;
}
public void setColor(int newColor) {
color = newColor;
}
public int getColor() {
return color;
}
}
class Line extends Shape {
private static int color = RED;
public static void
serializeStaticState(ObjectOutputStream os)
throws IOException {
os.writeInt(color);
}
public static void
deserializeStaticState(ObjectInputStream os)
throws IOException {
color = os.readInt();
}
public Line(int xVal, int yVal, int dim) {
super(xVal, yVal, dim);
}
public void setColor(int newColor) {
color = newColor;
}
public int getColor() {
return color;
}
}
public class CADState {
public static void main(String[] args)
throws Exception {
Vector shapeTypes, shapes;
if(args.length == 0) {
shapeTypes = new Vector();
shapes = new Vector();
// Add handles to the class objects:
shapeTypes.addElement(Circle.class);
shapeTypes.addElement(Square.class);
shapeTypes.addElement(Line.class);
// Make some shapes:
for(int i = 0; i < 10; i++)
shapes.addElement(Shape.randomFactory());
// Set all the static colors to GREEN:
for(int i = 0; i < 10; i++)
((Shape)shapes.elementAt(i))
.setColor(Shape.GREEN);
// Save the state vector:
ObjectOutputStream out =
new ObjectOutputStream(
new FileOutputStream("CADState.out"));
out.writeObject(shapeTypes);
Line.serializeStaticState(out);
out.writeObject(shapes);
} else { // There's a command-line argument
ObjectInputStream in =
new ObjectInputStream(
new FileInputStream(args[0]));
// Read in the same order they were written:
shapeTypes = (Vector)in.readObject();
Line.deserializeStaticState(in);
shapes = (Vector)in.readObject();
}
// Display the shapes:
System.out.println(shapes);
}
} ///:~

                                                                                                                                                        P.328

这段代码中把class对象序列化(out.writeObject(shapeTypes);和shapeTypes = (Vector)in.readObject();)起什么作用?

标签:dim,Java,--,yVal,int,static,序列化,public,color
From: https://blog.51cto.com/u_16183536/7652106

相关文章

  • python,机器学习,代码,三种方法,拟合,物料的冷却规律:温度Y=f(时间X,物料类型A,冷却方
    python,机器学习,代码,三种方法,拟合,物料的冷却规律:温度Y=f(时间X,物料类型A,冷却方式B)+其他因素理解你的问题,你似乎正在探讨如何使用机器学习方法对物料的冷却规律进行拟合,考虑到时间、物料类型、冷却方式以及其他因素。在这里,我将提供一个基本的框架,介绍三种常见的机器学习方......
  • 无涯教程-JavaScript - LEN函数
    描述LEN返回文本字符串中的字符数。LENB返回用于表示文本字符串中字符的字节数。仅当将DBCS语言设置为默认语言时,它每个字符计数2个字节。否则,LENB的行为与LEN相同,每个字符计数1个字节。支持DBCS的语言包括日语,中文(简体),中文(繁体)和韩语。语法LEN(text)LENB......
  • openGauss学习笔记-84 openGauss 数据库管理-内存优化表MOT管理-内存表特性-MOT部署服
    openGauss学习笔记-84openGauss数据库管理-内存优化表MOT管理-内存表特性-MOT部署服务器优化:x86通常情况下,数据库由以下组件绑定:CPU:更快的CPU可以加速任何CPU绑定的数据库。磁盘:高速SSD/NVME可加速任何I/O绑定数据库。网络:更快的网络可以加速任何SQL*Net绑定数据库。除以......
  • 视频融合平台/安防视频平台EasyCVR配合AI技术可有效降低高危职业风险
    在化工生产中,有八类特殊作业,包括动火作业、设备检维修内作业、高处安全作业、动土安全作业、断路安全作业、盲板抽堵安全作业、吊装安全作业和受限空间作业。这些作业相比一般行业具有更高的突然性、复杂性和危险性,由于环境易燃易爆且作业风险大,容易发生各类安全事故。另外,一些场......
  • 筛法求约数个数
    推荐视频:516筛法求约数个数点击查看代码#include<bits/stdc++.h>usingnamespacestd;#defineLLlonglongconstintN=1e8+10;intp[N],cnt;intd[N];//d[i]记录i的约数的个数inta[N];//a[i]记录i的最小质因子的次数boolvis[N];voidget_d(intn){//筛法求......
  • 实验1
    四、实验结论1.实验任务1task1_11#include<stdio.h>2intmain()3{4printf("O\n");5printf("<H>\n");6printf("II\n");78return0;9}  task1_21#include<stdio.h>2intmain()......
  • c#开发学习之listview连接SQLserver并将数据放到listview里显示
    usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceW......
  • 软件安装
    1.VMware安装网址:https://www.vmware.com/cn.html......
  • 7.数据库设计
    没有正儿八经的设计过数据库表结构,这注定是个需要长期更新的一篇文章~朋友推荐了一个工具,设计表还挺好用的:EZDML,有兴趣也可以试试~ 先从登录所需要的表开始吧:用户信息表:存储用户信息。状态信息表:用于管理用户状态或者文章状态。(例如封禁,屏蔽,启用,禁用诸如此类的,目前的想法......
  • SHELL——环境变量
    1、系统变量SHELL环境变量分类:作用域分类为全局变量和局部变量、系统变量和用户自定义变量。打印系统全局变量命令:env、printenv打印系统局部变量命令:set在编辑器中查看系统全局变量命令:env|less在编辑器中查看系统局部变量命令:set|less(全局变量可以在子shell中查看、......