Guest User

Untitled

a guest
Apr 20th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. public class Imp1 implements Inter1 {
  2. private int num;
  3.  
  4. @Override
  5. public void apply() {
  6. num++;
  7. }
  8.  
  9. public void doubler() {
  10. num = num * 2;
  11. }
  12.  
  13. public boolean equals(Imp1 o) {
  14. if (!(o instanceof Imp1))
  15. return false;
  16. return o.num == num;
  17. }
  18.  
  19. public int getNum() {
  20. return num;
  21. }
  22.  
  23. public static void main(String[] args) {
  24. final Inter1 a = new Imp1();
  25. final Imp1 b = new Imp1();
  26. a.apply();
  27. b.apply();
  28. System.out.println("a equals b " + a.equals(b));
  29. System.out.println("b equals a " + b.equals(a));
  30. }
  31.  
  32. }
  33.  
  34. System.out.println("a equals b " + ((Imp1)a).equals(b));
  35.  
  36. public class Imp1 implements Inter1 {
  37. private int num;
  38.  
  39. @Override
  40. public void apply() {
  41. num++;
  42. }
  43.  
  44. public void doubler() {
  45. num = num * 2;
  46. }
  47.  
  48. @Override//<-----need to override the equals of the superClass Object!!!
  49. public boolean equals(Object o) {//<---must be Object!!!
  50. if (!(o instanceof Imp1))
  51. return false;
  52. return ((Imp1)o).num == num;
  53. }
  54.  
  55. public int getNum() {
  56. return num;
  57. }
  58.  
  59. public static void main(String[] args) {
  60. final Inter1 a = new Imp1();
  61. final Imp1 b = new Imp1();
  62. a.apply();
  63. b.apply();
  64. System.out.println("a equals b " + a.equals(b));
  65. System.out.println("b equals a " + b.equals(a));
  66. }
  67.  
  68. }
  69.  
  70. public class Imp1 implements Inter1 {
  71. private int num;
  72.  
  73. @Override
  74. public void apply() {
  75. num++;
  76. }
  77.  
  78. public void doubler() {
  79. num = num * 2;
  80. }
  81.  
  82. @Override //<---------------need to be implemented from the interface!
  83. public boolean equals(Inter1 o) {//<----the class must to be from the specific interface!!!
  84. if (!(o instanceof Imp1))
  85. return false;
  86. return ((Imp1)o).num == num;//<-----must to cast the object to the specific object in the "if"
  87. }
  88.  
  89. public int getNum() {
  90. return num;
  91. }
  92.  
  93. public static void main(String[] args) {
  94. final Inter1 a = new Imp1();
  95. final Imp1 b = new Imp1();
  96. a.apply();
  97. b.apply();
  98. System.out.println("a equals b " + a.equals(b));
  99. System.out.println("b equals a " + b.equals(a));
  100. }
  101.  
  102. }
Add Comment
Please, Sign In to add comment