Задача: "The Java Programming Language" Ken Arnold, James Gosling, David Holmes листинги, код, примеры из книги, исходники
Исходник: Глава 15, Ввод-Вывод (Chapter 15. Input-Output), Rectangle class, язык: java [code #186, hits: 8393]
автор: - [добавлен: 15.09.2006]
  1. import java.io.*;
  2.  
  3. /**
  4. * Created by IntelliJ IDEA.
  5. * User: me
  6. * Date: 15.09.2006
  7. * Time: 17:18:46
  8. * To change this template use File | Settings | File Templates.
  9. */
  10. public class Rectangle implements Serializable {
  11. private static final long
  12. serialVersionUID = -1300779387465845764L;
  13.  
  14. private double x1,y1,x2,y2;
  15. transient private double x, y;
  16. transient private double width, height;
  17.  
  18. public Rectangle(double x, double y, double width, double height) {
  19. this.x = x;
  20. this.y = y;
  21. this.width = width;
  22. this.height = height;
  23. }
  24.  
  25. private void readObject(ObjectInputStream in)
  26. {
  27. ObjectInputStream.GetField fields;
  28.  
  29. fields = in.readFields();
  30. x = fields.get("x1", 0.0);
  31. y = fields.get("y1", 0.0);
  32. double x2 = fields.get("x2", 0.0);
  33. double y2 = fields.get("y2", 0.0);
  34. width = (x2 - x);
  35. height = (y2 - y);
  36.  
  37. System.out.println("readObject is called!");
  38. }
  39.  
  40. private void writeObject(ObjectOutputStream out)
  41. throws IOException
  42. {
  43. ObjectOutputStream.PutField fields = out.putFields();
  44.  
  45. fields.put("x1", x);
  46. fields.put("y1", y);
  47. fields.put("x2", x + width);
  48. fields.put("y2", y + height);
  49.  
  50. System.out.println("writeObject is called!");
  51. }
  52.  
  53. private static final ObjectStreamField[]
  54. SerialPersistentFields = {
  55. new ObjectStreamField("x1", Double.TYPE),
  56. new ObjectStreamField("y1", Double.TYPE),
  57. new ObjectStreamField("x2", Double.TYPE),
  58. new ObjectStreamField("y2", Double.TYPE)
  59. };
  60.  
  61. public String toString() {
  62. return "x:" + x + " y:" + y + " width:" + width +
  63. " height:" + height + " x1:" + x1 + " x2:" + x2 + " y1:" + y1 + " y2:" + y2;
  64. }
  65. }
Тестировалось на: java 1.5.0_04

+добавить реализацию