"C:\Program Files\Java\jdk1.8.0_171\bin\java.exe" ...
nameA: AB2B2, nameB: C33
nameA: AB2B2, nameB: C33
Process finished with exit code 0
5. Программа должна выполняться без ошибок. - exit code 0 - норм!
6. При десериализации должны корректно восстанавливаться значение полей nameA и nameB. - nameA: AB2B2, nameB: C33 - норм!
Так что не так-то?
package com.javarush.task.task20.task2018;
import java.io.*;
/*
Найти ошибки
*/
public class Solution {
public static class A {
protected String nameA = "A";
public A() {
}
public A (String nameA) {
this.nameA += nameA;
}
}
public class B extends A implements Serializable {
private String nameB;
public B(String nameA, String nameB) {
super(nameA);
this.nameA += nameA;
this.nameB = nameB;
}
private void writeObject(ObjectOutputStream out) throws IOException {
out.writeObject(nameA);
out.writeObject(nameB);
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
nameA = (String) in.readObject();
nameB = (String) in.readObject();
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(arrayOutputStream);
Solution solution = new Solution();
B b = solution.new B("B2", "C33");
System.out.println("nameA: " + b.nameA + ", nameB: " + b.nameB);
oos.writeObject(b);
ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(arrayOutputStream.toByteArray());
ObjectInputStream ois = new ObjectInputStream(arrayInputStream);
B b1 = (B) ois.readObject();
System.out.println("nameA: " + b1.nameA + ", nameB: " + b1.nameB);
}
}