fork(3) download
  1. import java.util.*;
  2. import java.lang.*;
  3.  
  4. class Main
  5. {
  6. public static void main (String[] args) throws java.lang.Exception
  7. {
  8. System.out.println("abc");
  9.  
  10. Something st1 = new Something(1,2.3);
  11. Something st2 = new Something(4,5.6);
  12.  
  13. st1.print();
  14. st2.print();
  15.  
  16. //st1.swap(st2);
  17. swap(st1, st2);
  18.  
  19. st1.print();
  20. st2.print();
  21. }
  22.  
  23. private static <T extends Swappable> void swap(T a, T b)
  24. {
  25. a.swap(b);
  26. }
  27. }
  28.  
  29. interface Swappable<T extends Swappable>
  30. {
  31. public void swap(T other);
  32. }
  33.  
  34. class Something implements Swappable<Something>
  35. {
  36. private int i;
  37. private double d;
  38.  
  39. public Something(int _i, double _d)
  40. {
  41. this.i = _i;
  42. this.d = _d;
  43. }
  44.  
  45. public void print()
  46. {
  47. System.out.println("i: " + i + "\r\nd: " + d + "\r\n");
  48. }
  49.  
  50. public void swap(Something other)
  51. {
  52. int _i = other.i;
  53. double _d = other.d;
  54.  
  55. other.i = this.i;
  56. other.d = this.d;
  57.  
  58. this.i = _i;
  59. this.d = _d;
  60. }
  61. }
Success #stdin #stdout 0.09s 213440KB
stdin
Standard input is empty
stdout
abc
i: 1
d: 2.3

i: 4
d: 5.6

i: 4
d: 5.6

i: 1
d: 2.3