方法重写
1 public class Equals { 2 private String nume; 3 private String sex; 4 5 public Equals(String nume,String sex ) { 6 this.nume = nume; 7 this.sex=sex; 8 } 9 10 public String getSex() { 11 return sex; 12 } 13 14 public void setSex(String sex) { 15 this.sex = sex; 16 } 17 18 public String getNume() { 19 return nume; 20 } 21 22 public void setNume(String nume) { 23 this.nume = nume; 24 } 25 26 public boolean equals(Object obj){ 27 if(obj instanceof Equals){ 28 Equals e =(Equals)obj; 29 if(e.getNume().equals(this.getNume())&&e.getSex().equals(this.getSex())){ 30 return true; 31 } 32 } 33 return false; 34 } 35 }
1 public class equalsTest { 2 public static void main(String[] args){ 3 Equals e1 = new Equals("小黄","16"); 4 Equals e2 = new Equals("小黄","16"); 5 System.out.println(e1.equals(e2)); 6 System.out.println("17".equals(17)); 7 } 8 }