我的代码的错误结果
Wrong result of my code
我读了 Java 中的思考。我正在阅读第 I/O.
章
按照书上的,然后我读取序列化后的文件"CADState.out",无法正确读取。
因为 Bruce Eckel 说:"It’s as if the statics didn’t get serialized at all! ... So if you want to serialize statics, you must do it yourself."
我已经在我的电脑上多次测试这段代码,但它序列化正确。
也许有人可以帮助我?
(我的电脑 - Java 版本 7 更新 67,Mac OS X 10.9.4)
package IO;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
abstract class Shape implements Serializable {
public static final int RED = 1, BLUE = 2, GREEN = 3;
private int xPos, yPos, dimension;
private static Random rand = new Random(47);
private static int counter = 0;
public abstract void setColor(int newColor);
public abstract int getColor();
public Shape(int xVal, int yVal, int dim) {
xPos = xVal;
yPos = yVal;
dimension = dim;
}
public String toString() {
return getClass() +
"color[" + getColor() + "] xPos[" + xPos +
"] yPos[" + yPos + "] dim[" + dimension + "]\n";
}
public static Shape randomFactory() {
int xVal = rand.nextInt(100);
int yVal = rand.nextInt(100);
int dim = rand.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 Thirty {
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
List<Class<? extends Shape>> shapeTypes =
new ArrayList<Class<? extends Shape>>();
// Add references to the class objects:
shapeTypes.add(Circle.class);
shapeTypes.add(Square.class);
shapeTypes.add(Line.class);
List<Shape> shapes = new ArrayList<Shape>();
// Make some shapes:
for(int i = 0; i < 10; i++)
shapes.add(Shape.randomFactory());
// Set all the static colors to GREEN:
for(int i = 0; i < 10; i++)
((Shape)shapes.get(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);
// Display the shapes:
System.out.println(shapes);
ObjectInputStream in = new ObjectInputStream(new FileInputStream("CADState.out"));
// Read in the same order they were written:
@SuppressWarnings("unchecked")
List<Class<? extends Shape>> shapeTypes2 = (List<Class<? extends Shape>>)in.readObject();
Line.deserializeStaticState(in);
@SuppressWarnings("unchecked")
List<Shape> shapes2 = (List<Shape>)in.readObject();
System.out.println(shapes2);
}
}
根据书本代码输出:
[class Circlecolor[3] xPos[58] yPos[55] dim[93]
, class Squarecolor[3] xPos[61] yPos[61] dim[29]
, class Linecolor[3] xPos[68] yPos[0] dim[22]
, class Circlecolor[3] xPos[7] yPos[88] dim[28]
, class Squarecolor[3] xPos[51] yPos[89] dim[9]
, class Linecolor[3] xPos[78] yPos[98] dim[61]
, class Circlecolor[3] xPos[20] yPos[58] dim[16]
, class Squarecolor[3] xPos[40] yPos[11] dim[22]
, class Linecolor[3] xPos[4] yPos[83] dim[6]
, class Circlecolor[3] xPos[75] yPos[10] dim[42]
]
[class Circlecolor[1] xPos[58] yPos[55] dim[93]
, class Squarecolor[0] xPos[61] yPos[61] dim[29]
, class Linecolor[3] xPos[68] yPos[0] dim[22]
, class Circlecolor[1] xPos[7] yPos[88] dim[28]
, class Squarecolor[0] xPos[51] yPos[89] dim[9]
, class Linecolor[3] xPos[78] yPos[98] dim[61]
, class Circlecolor[1] xPos[20] yPos[58] dim[16]
, class Squarecolor[0] xPos[40] yPos[11] dim[22]
, class Linecolor[3] xPos[4] yPos[83] dim[6]
, class Circlecolor[1] xPos[75] yPos[10] dim[42]
在我的电脑上输出这段代码:
[class IO.Circlecolor[3] xPos[58] yPos[55] dim[93]
, class IO.Squarecolor[3] xPos[61] yPos[61] dim[29]
, class IO.Linecolor[3] xPos[68] yPos[0] dim[22]
, class IO.Circlecolor[3] xPos[7] yPos[88] dim[28]
, class IO.Squarecolor[3] xPos[51] yPos[89] dim[9]
, class IO.Linecolor[3] xPos[78] yPos[98] dim[61]
, class IO.Circlecolor[3] xPos[20] yPos[58] dim[16]
, class IO.Squarecolor[3] xPos[40] yPos[11] dim[22]
, class IO.Linecolor[3] xPos[4] yPos[83] dim[6]
, class IO.Circlecolor[3] xPos[75] yPos[10] dim[42]
]
[class IO.Circlecolor[3] xPos[58] yPos[55] dim[93]
, class IO.Squarecolor[3] xPos[61] yPos[61] dim[29]
, class IO.Linecolor[3] xPos[68] yPos[0] dim[22]
, class IO.Circlecolor[3] xPos[7] yPos[88] dim[28]
, class IO.Squarecolor[3] xPos[51] yPos[89] dim[9]
, class IO.Linecolor[3] xPos[78] yPos[98] dim[61]
, class IO.Circlecolor[3] xPos[20] yPos[58] dim[16]
, class IO.Squarecolor[3] xPos[40] yPos[11] dim[22]
, class IO.Linecolor[3] xPos[4] yPos[83] dim[6]
, class IO.Circlecolor[3] xPos[75] yPos[10] dim[42]
]
我发现了我的错误。谢谢那些家伙 - http://www.coderanch.com/t/546558/java-io/java/Serialization-static-variables。
Rob Spoor:变量 y 是静态的。这意味着只要 class 被加载,它就会保持相同的值。您在同一个 JVM 实例中进行序列化和反序列化。尝试将序列化和反序列化拆分为两个 classes 并查看差异。
我已将代码分成两个 classes:Thirty 和 ThirtyRead。之后,一切开始正常工作。坦率地说,在书中也有两个 classes ))。但是,我没有注意到它。我把所有东西都放在同一个 class.
这里解决。 Class三十:
package IO;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
abstract class Shape implements Serializable {
public static final int RED = 1, BLUE = 2, GREEN = 3;
private int xPos, yPos, dimension;
private static Random rand = new Random(47);
private static int counter = 0;
public abstract void setColor(int newColor);
public abstract int getColor();
public Shape(int xVal, int yVal, int dim) {
xPos = xVal;
yPos = yVal;
dimension = dim;
}
public String toString() {
return getClass() +
"color[" + getColor() + "] xPos[" + xPos +
"] yPos[" + yPos + "] dim[" + dimension + "]\n";
}
public static Shape randomFactory() {
int xVal = rand.nextInt(100);
int yVal = rand.nextInt(100);
int dim = rand.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 Thirty {
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
List<Class<? extends Shape>> shapeTypes =
new ArrayList<Class<? extends Shape>>();
// Add references to the class objects:
shapeTypes.add(Circle.class);
shapeTypes.add(Square.class);
shapeTypes.add(Line.class);
List<Shape> shapes = new ArrayList<Shape>();
// Make some shapes:
for(int i = 0; i < 10; i++)
shapes.add(Shape.randomFactory());
// Set all the static colors to GREEN:
for(int i = 0; i < 10; i++)
((Shape)shapes.get(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);
// Display the shapes:
System.out.println(shapes);
}
}
// output
[class IO.Circlecolor[3] xPos[58] yPos[55] dim[93]
, class IO.Squarecolor[3] xPos[61] yPos[61] dim[29]
, class IO.Linecolor[3] xPos[68] yPos[0] dim[22]
, class IO.Circlecolor[3] xPos[7] yPos[88] dim[28]
, class IO.Squarecolor[3] xPos[51] yPos[89] dim[9]
, class IO.Linecolor[3] xPos[78] yPos[98] dim[61]
, class IO.Circlecolor[3] xPos[20] yPos[58] dim[16]
, class IO.Squarecolor[3] xPos[40] yPos[11] dim[22]
, class IO.Linecolor[3] xPos[4] yPos[83] dim[6]
, class IO.Circlecolor[3] xPos[75] yPos[10] dim[42]
]
Class 三十读
package IO;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.List;
public class ThirtyRead {
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
// TODO Auto-generated method stub
ObjectInputStream in = new ObjectInputStream(new FileInputStream("CADState.out"));
// Read in the same order they were written:
@SuppressWarnings("unchecked")
List<Class<? extends Shape>> shapeTypes2 = (List<Class<? extends Shape>>)in.readObject();
Line.deserializeStaticState(in);
@SuppressWarnings("unchecked")
List<Shape> shapes2 = (List<Shape>)in.readObject();
System.out.println(shapes2);
}
}
// output
[class IO.Circlecolor[1] xPos[58] yPos[55] dim[93]
, class IO.Squarecolor[0] xPos[61] yPos[61] dim[29]
, class IO.Linecolor[3] xPos[68] yPos[0] dim[22]
, class IO.Circlecolor[1] xPos[7] yPos[88] dim[28]
, class IO.Squarecolor[0] xPos[51] yPos[89] dim[9]
, class IO.Linecolor[3] xPos[78] yPos[98] dim[61]
, class IO.Circlecolor[1] xPos[20] yPos[58] dim[16]
, class IO.Squarecolor[0] xPos[40] yPos[11] dim[22]
, class IO.Linecolor[3] xPos[4] yPos[83] dim[6]
, class IO.Circlecolor[1] xPos[75] yPos[10] dim[42]
]
我读了 Java 中的思考。我正在阅读第 I/O.
章
按照书上的,然后我读取序列化后的文件"CADState.out",无法正确读取。
因为 Bruce Eckel 说:"It’s as if the statics didn’t get serialized at all! ... So if you want to serialize statics, you must do it yourself."
我已经在我的电脑上多次测试这段代码,但它序列化正确。
也许有人可以帮助我?
(我的电脑 - Java 版本 7 更新 67,Mac OS X 10.9.4)
package IO;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
abstract class Shape implements Serializable {
public static final int RED = 1, BLUE = 2, GREEN = 3;
private int xPos, yPos, dimension;
private static Random rand = new Random(47);
private static int counter = 0;
public abstract void setColor(int newColor);
public abstract int getColor();
public Shape(int xVal, int yVal, int dim) {
xPos = xVal;
yPos = yVal;
dimension = dim;
}
public String toString() {
return getClass() +
"color[" + getColor() + "] xPos[" + xPos +
"] yPos[" + yPos + "] dim[" + dimension + "]\n";
}
public static Shape randomFactory() {
int xVal = rand.nextInt(100);
int yVal = rand.nextInt(100);
int dim = rand.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 Thirty {
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
List<Class<? extends Shape>> shapeTypes =
new ArrayList<Class<? extends Shape>>();
// Add references to the class objects:
shapeTypes.add(Circle.class);
shapeTypes.add(Square.class);
shapeTypes.add(Line.class);
List<Shape> shapes = new ArrayList<Shape>();
// Make some shapes:
for(int i = 0; i < 10; i++)
shapes.add(Shape.randomFactory());
// Set all the static colors to GREEN:
for(int i = 0; i < 10; i++)
((Shape)shapes.get(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);
// Display the shapes:
System.out.println(shapes);
ObjectInputStream in = new ObjectInputStream(new FileInputStream("CADState.out"));
// Read in the same order they were written:
@SuppressWarnings("unchecked")
List<Class<? extends Shape>> shapeTypes2 = (List<Class<? extends Shape>>)in.readObject();
Line.deserializeStaticState(in);
@SuppressWarnings("unchecked")
List<Shape> shapes2 = (List<Shape>)in.readObject();
System.out.println(shapes2);
}
}
根据书本代码输出:
[class Circlecolor[3] xPos[58] yPos[55] dim[93]
, class Squarecolor[3] xPos[61] yPos[61] dim[29]
, class Linecolor[3] xPos[68] yPos[0] dim[22]
, class Circlecolor[3] xPos[7] yPos[88] dim[28]
, class Squarecolor[3] xPos[51] yPos[89] dim[9]
, class Linecolor[3] xPos[78] yPos[98] dim[61]
, class Circlecolor[3] xPos[20] yPos[58] dim[16]
, class Squarecolor[3] xPos[40] yPos[11] dim[22]
, class Linecolor[3] xPos[4] yPos[83] dim[6]
, class Circlecolor[3] xPos[75] yPos[10] dim[42]
]
[class Circlecolor[1] xPos[58] yPos[55] dim[93]
, class Squarecolor[0] xPos[61] yPos[61] dim[29]
, class Linecolor[3] xPos[68] yPos[0] dim[22]
, class Circlecolor[1] xPos[7] yPos[88] dim[28]
, class Squarecolor[0] xPos[51] yPos[89] dim[9]
, class Linecolor[3] xPos[78] yPos[98] dim[61]
, class Circlecolor[1] xPos[20] yPos[58] dim[16]
, class Squarecolor[0] xPos[40] yPos[11] dim[22]
, class Linecolor[3] xPos[4] yPos[83] dim[6]
, class Circlecolor[1] xPos[75] yPos[10] dim[42]
在我的电脑上输出这段代码:
[class IO.Circlecolor[3] xPos[58] yPos[55] dim[93]
, class IO.Squarecolor[3] xPos[61] yPos[61] dim[29]
, class IO.Linecolor[3] xPos[68] yPos[0] dim[22]
, class IO.Circlecolor[3] xPos[7] yPos[88] dim[28]
, class IO.Squarecolor[3] xPos[51] yPos[89] dim[9]
, class IO.Linecolor[3] xPos[78] yPos[98] dim[61]
, class IO.Circlecolor[3] xPos[20] yPos[58] dim[16]
, class IO.Squarecolor[3] xPos[40] yPos[11] dim[22]
, class IO.Linecolor[3] xPos[4] yPos[83] dim[6]
, class IO.Circlecolor[3] xPos[75] yPos[10] dim[42]
]
[class IO.Circlecolor[3] xPos[58] yPos[55] dim[93]
, class IO.Squarecolor[3] xPos[61] yPos[61] dim[29]
, class IO.Linecolor[3] xPos[68] yPos[0] dim[22]
, class IO.Circlecolor[3] xPos[7] yPos[88] dim[28]
, class IO.Squarecolor[3] xPos[51] yPos[89] dim[9]
, class IO.Linecolor[3] xPos[78] yPos[98] dim[61]
, class IO.Circlecolor[3] xPos[20] yPos[58] dim[16]
, class IO.Squarecolor[3] xPos[40] yPos[11] dim[22]
, class IO.Linecolor[3] xPos[4] yPos[83] dim[6]
, class IO.Circlecolor[3] xPos[75] yPos[10] dim[42]
]
我发现了我的错误。谢谢那些家伙 - http://www.coderanch.com/t/546558/java-io/java/Serialization-static-variables。 Rob Spoor:变量 y 是静态的。这意味着只要 class 被加载,它就会保持相同的值。您在同一个 JVM 实例中进行序列化和反序列化。尝试将序列化和反序列化拆分为两个 classes 并查看差异。
我已将代码分成两个 classes:Thirty 和 ThirtyRead。之后,一切开始正常工作。坦率地说,在书中也有两个 classes ))。但是,我没有注意到它。我把所有东西都放在同一个 class.
这里解决。 Class三十:
package IO;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
abstract class Shape implements Serializable {
public static final int RED = 1, BLUE = 2, GREEN = 3;
private int xPos, yPos, dimension;
private static Random rand = new Random(47);
private static int counter = 0;
public abstract void setColor(int newColor);
public abstract int getColor();
public Shape(int xVal, int yVal, int dim) {
xPos = xVal;
yPos = yVal;
dimension = dim;
}
public String toString() {
return getClass() +
"color[" + getColor() + "] xPos[" + xPos +
"] yPos[" + yPos + "] dim[" + dimension + "]\n";
}
public static Shape randomFactory() {
int xVal = rand.nextInt(100);
int yVal = rand.nextInt(100);
int dim = rand.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 Thirty {
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
List<Class<? extends Shape>> shapeTypes =
new ArrayList<Class<? extends Shape>>();
// Add references to the class objects:
shapeTypes.add(Circle.class);
shapeTypes.add(Square.class);
shapeTypes.add(Line.class);
List<Shape> shapes = new ArrayList<Shape>();
// Make some shapes:
for(int i = 0; i < 10; i++)
shapes.add(Shape.randomFactory());
// Set all the static colors to GREEN:
for(int i = 0; i < 10; i++)
((Shape)shapes.get(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);
// Display the shapes:
System.out.println(shapes);
}
}
// output
[class IO.Circlecolor[3] xPos[58] yPos[55] dim[93]
, class IO.Squarecolor[3] xPos[61] yPos[61] dim[29]
, class IO.Linecolor[3] xPos[68] yPos[0] dim[22]
, class IO.Circlecolor[3] xPos[7] yPos[88] dim[28]
, class IO.Squarecolor[3] xPos[51] yPos[89] dim[9]
, class IO.Linecolor[3] xPos[78] yPos[98] dim[61]
, class IO.Circlecolor[3] xPos[20] yPos[58] dim[16]
, class IO.Squarecolor[3] xPos[40] yPos[11] dim[22]
, class IO.Linecolor[3] xPos[4] yPos[83] dim[6]
, class IO.Circlecolor[3] xPos[75] yPos[10] dim[42]
]
Class 三十读
package IO;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.List;
public class ThirtyRead {
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
// TODO Auto-generated method stub
ObjectInputStream in = new ObjectInputStream(new FileInputStream("CADState.out"));
// Read in the same order they were written:
@SuppressWarnings("unchecked")
List<Class<? extends Shape>> shapeTypes2 = (List<Class<? extends Shape>>)in.readObject();
Line.deserializeStaticState(in);
@SuppressWarnings("unchecked")
List<Shape> shapes2 = (List<Shape>)in.readObject();
System.out.println(shapes2);
}
}
// output
[class IO.Circlecolor[1] xPos[58] yPos[55] dim[93]
, class IO.Squarecolor[0] xPos[61] yPos[61] dim[29]
, class IO.Linecolor[3] xPos[68] yPos[0] dim[22]
, class IO.Circlecolor[1] xPos[7] yPos[88] dim[28]
, class IO.Squarecolor[0] xPos[51] yPos[89] dim[9]
, class IO.Linecolor[3] xPos[78] yPos[98] dim[61]
, class IO.Circlecolor[1] xPos[20] yPos[58] dim[16]
, class IO.Squarecolor[0] xPos[40] yPos[11] dim[22]
, class IO.Linecolor[3] xPos[4] yPos[83] dim[6]
, class IO.Circlecolor[1] xPos[75] yPos[10] dim[42]
]