gpt4 book ai didi

java - Java 的 XMLEncoder 可以序列化具有不可序列化字段的对象吗?

转载 作者:行者123 更新时间:2023-12-01 19:55:16 25 4
gpt4 key购买 nike

我对书中甚至在这里找到的所有观点感到非常困惑,所以请耐心等待!

Clay Richardson 的 Professional Java JDK 6 中指出

you will notice that MyBean does not implement Serializable. XMLEncoder/Decoder does not require classes it serializes to implement Serializable.

还有那个

the XMLEncoder/Decoder API serializes object instances without any knowledge of their private data members. It serializes based upon the object’s methods, its JavaBean properties, exposed through the JavaBeans convention > of getters and setters (getXXX and setXXX).

这是否意味着 XMLEncoder 可以序列化不可序列化的对象,例如 javafx.scence.color(如果它是 JavaBean 内的字段)?好吧,我尝试创建一个 JavaBean 并遵循有关命名和默认无参数构造函数的所有规定约定,如下所示

import javafx.geometry.Point2D;
import javafx.scene.paint.Color;
import java.io.Serializable;
import java.util.Map;

public class PCircle extends Shape implements Serializable {

private Point2D position;
private Map<String, Double> properties;
private Color backColor;
private Color strokeColor;

public void setPosition(Point2D position) { this.position = position; }
public Point2D getPosition() { return this.position; }


public void setProperties(Map<String, Double> properties) { this.properties = properties; }
public Map<String, Double> getProperties() { return this.properties; }


public void setBackColor(Color color) { this.backColor = color; }
public Color getBackColor() { return this.backColor; }

public void setStrokeColor(Color color) { this.strokeColor = color; }
public Color getStrokeColor() { return this.strokeColor; }

}

其中Shape是一个实现IShape接口(interface)的抽象类,如下所示

import javafx.geometry.Point2D;
import javafx.scene.paint.Color;
import java.util.Map;

public interface IShape {

void setPosition(Point2D position);
Point2D getPosition();

void setProperties(Map<String, Double> properties);
Map<String, Double> getProperties();

void setBackColor(Color color);
Color getBackColor();

void setStrokeColor(Color color);
Color getStrokeColor();

}

public abstract class Shape implements IShape {

abstract public void setPosition(Point2D position);
abstract public Point2D getPosition();


abstract public void setProperties(Map<String, Double> properties);
abstract public Map<String, Double> getProperties();

abstract public void setBackColor(Color color);
abstract public Color getBackColor();

abstract public void setStrokeColor(Color color);
abstract public Color getStrokeColor();

}

现在如果我运行这个主要方法来测试

import javafx.geometry.Point2D;
import javafx.scene.paint.Color;
import java.beans.XMLEncoder;
import java.io.FileOutputStream;
import java.util.HashMap;

public class Main {

public static void main(String[] args) {

HashMap<String, Double> properties = new HashMap<>();
properties.put("First Property", 20.5);

PCircle circle = new PCircle();

circle.setPosition(new Point2D(20, 30));
circle.setProperties(properties);
circle.setBackColor(Color.BLACK);
circle.setStrokeColor(Color.RED);

try {
FileOutputStream fos = new FileOutputStream("output.xml");
XMLEncoder encoder = new XMLEncoder(fos);
encoder.writeObject(circle);
encoder.close();
fos.close();
} catch (Exception ex) {
System.out.println("Exception! " + ex.getMessage());
}

}
}

我收到此错误

java.lang.InstantiationException: javafx.scene.paint.Color
Continuing ...
java.lang.RuntimeException: failed to evaluate: <unbound>=Class.new();
Continuing ...
java.lang.InstantiationException: javafx.scene.paint.Color
Continuing ...
java.lang.RuntimeException: failed to evaluate: <unbound>=Class.new();
Continuing ...
java.lang.InstantiationException: javafx.scene.paint.Color
Continuing ...
java.lang.RuntimeException: failed to evaluate: <unbound>=Class.new();
Continuing ...
java.lang.InstantiationException: javafx.scene.paint.Color
Continuing ...
java.lang.RuntimeException: failed to evaluate: <unbound>=Class.new();
Continuing ...

有趣的是,如果我使用可序列化的类,例如 java.awt.Color,代码可以正常工作并生成 XML。但是,即使明确指出 JavaBean 不需要实现 Serialized 接口(interface),这又有什么关系呢?

最佳答案

Doesn't that mean that XMLEncoder can serialize non-serializable objects such as a javafx.scene.[paint.]color if it is a field inside a JavaBean?

没有。这意味着它可以序列化不可序列化的对象,例如 javafx.scene.paint.color 如果它们也遵守 JavaBeans 约定,并且提供它们的对象值属性以此类推,直到关闭。

在本例中,javafx.scene.paint.color 没有无参数构造函数。 java.awt.Color 之所以有效,是因为它遵守 JavaBeans 约定,而不是因为它是Serialized

关于java - Java 的 XMLEncoder 可以序列化具有不可序列化字段的对象吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49868249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com