我有一个类Den
实现Serialized
接口(interface):
public class Den implements Serializable {
private double id;
private String name;
public Den(double id, String name){
this.id = id;
this.name = name;
}
public double getId() {
return id;
}
public void setId(double id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
我可以在 Intent
中发送一个 Den
实例,如下所示:
Intent intent = new Intent(context, NewActivity.class);
intent.putExtra("OBJECT_KEY", new Den(20.2, "JOHN"));
startActivity(intent);
并在另一个Activity
中接收它,如下所示:
Intent intent = getIntent();
Den den = (Den) intent.getSerializableExtra("OBJECT_KEY");
现在我的问题是:为什么我需要在 Den
上实现 Serializable
接口(interface),而不是在 int
这样的原始数据类型上实现, float
还是字符串
?内部发生了什么?
最佳答案
Why do I need to implement the Serializable interface on Den, but not on primitive data types like int, float or String?
String 是可序列化的且非原始类型。
原始类型无法实现接口(interface)。
序列化对象时,JVM 会处理原始类型的字段。
关于java - 对 Serialized 的内部工作原理感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32089248/