在java序列化类中,Mp3player扩展了ElectronicDevice实现Serializable,在这段代码中,超类electronicdevice未实现可序列化.这里超类也被序列化了.我的理解是超级类也因为extends.let而被序列化.我知道我的理解是否正确.
import java.io.*;
class ElectronicDevice {
ElectronicDevice()
{
System.out.print("ed ");
}
}
class Mp3player extends ElectronicDevice implements Serializable {
Mp3player()
{
System.out.print("mp ");
}
}
class MiniPlayer extends Mp3player {
MiniPlayer()
{
System.out.print("mini ");
}
public static void main(String[] args) {
MiniPlayer m = new MiniPlayer();
try {
FileOutputStream fos = new FileOutputStream("dev.txt");
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(m); os.close();
FileInputStream fis = new FileInputStream("dev.txt");
ObjectInputStream is = new ObjectInputStream(fis);
MiniPlayer m2 = (MiniPlayer) is.readObject();
is.close();
System.out.println();
} catch (Exception …Run Code Online (Sandbox Code Playgroud) 我正在尝试序列化我的位置类(使用android.location类)
但是,它给了我一个错误!
11-21 21:25:37.337: W/System.err(3152): java.io.NotSerializableException: android.location
Run Code Online (Sandbox Code Playgroud)
所以,我试图扩展android.location.Location课程.
private class NewLocation extends Location implements Serializable {
private String Provider;
private double Latitude, Longitude, Altitude;
private float bear;
public NewLocation(Location l) {
super(l);
Provider = l.getProvider();
Latitude = l.getLatitude();
Longitude = l.getLongitude();
Altitude = l.getAltitude();
bear = l.getBearing();
}
}
Run Code Online (Sandbox Code Playgroud)
之后,我试图序列化扩展类,但同样的错误.
这是序列化代码
public static byte[] serialize(Object obj) throws IOException {
ByteArrayOutputStream bao = new ByteArrayOutputStream();
ObjectOutput oos = new ObjectOutputStream(bao);
byte[] data = null;
oos.writeObject(obj);
oos.flush();
oos.close();
data = …Run Code Online (Sandbox Code Playgroud)