import java.io.*;
public class SuperNotSerial {
public static void main(String[] args) {
Dog d = new Dog(35, "Fido");
System.out.println("before: " + d.name + " " + d.weight);
try {
FileOutputStream fs = new FileOutputStream("testSer.ser");
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(d);
os.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
FileInputStream fis = new FileInputStream("testSer.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
d = (Dog) ois.readObject();
ois.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("after: " + d.name + " " + d.weight);
}
}
class Dog extends Animal implements Serializable {
String name;
Dog(int w, String n) {
weight = w; // inherited
name = n; // not inherited
}
}
class Animal { // not serializable !
int weight = 42;
}
即將發生的錯誤是。運行時錯誤NoSuchMethodError
此錯誤是編譯錯誤,我無法理解確切原因
Exception in thread "main" java.lang.NoSuchMethodError: Dog.<init> (ILjava/lang/String;)V
at SuperNotSerial.main(SuperNotSerial.java:5)
這對我來說看起來不像編譯器錯誤。 – Patashu
適合我... –
適用於我。我之前得到了這個:Fido 35 之後:Fido 42 – Nargis