Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Imp1 implements Inter1 {
- private int num;
- @Override
- public void apply() {
- num++;
- }
- public void doubler() {
- num = num * 2;
- }
- public boolean equals(Imp1 o) {
- if (!(o instanceof Imp1))
- return false;
- return o.num == num;
- }
- public int getNum() {
- return num;
- }
- public static void main(String[] args) {
- final Inter1 a = new Imp1();
- final Imp1 b = new Imp1();
- a.apply();
- b.apply();
- System.out.println("a equals b " + a.equals(b));
- System.out.println("b equals a " + b.equals(a));
- }
- }
- System.out.println("a equals b " + ((Imp1)a).equals(b));
- public class Imp1 implements Inter1 {
- private int num;
- @Override
- public void apply() {
- num++;
- }
- public void doubler() {
- num = num * 2;
- }
- @Override//<-----need to override the equals of the superClass Object!!!
- public boolean equals(Object o) {//<---must be Object!!!
- if (!(o instanceof Imp1))
- return false;
- return ((Imp1)o).num == num;
- }
- public int getNum() {
- return num;
- }
- public static void main(String[] args) {
- final Inter1 a = new Imp1();
- final Imp1 b = new Imp1();
- a.apply();
- b.apply();
- System.out.println("a equals b " + a.equals(b));
- System.out.println("b equals a " + b.equals(a));
- }
- }
- public class Imp1 implements Inter1 {
- private int num;
- @Override
- public void apply() {
- num++;
- }
- public void doubler() {
- num = num * 2;
- }
- @Override //<---------------need to be implemented from the interface!
- public boolean equals(Inter1 o) {//<----the class must to be from the specific interface!!!
- if (!(o instanceof Imp1))
- return false;
- return ((Imp1)o).num == num;//<-----must to cast the object to the specific object in the "if"
- }
- public int getNum() {
- return num;
- }
- public static void main(String[] args) {
- final Inter1 a = new Imp1();
- final Imp1 b = new Imp1();
- a.apply();
- b.apply();
- System.out.println("a equals b " + a.equals(b));
- System.out.println("b equals a " + b.equals(a));
- }
- }
Add Comment
Please, Sign In to add comment