代码1;
public class Equals { private String nume; private String sex; public Equals(String nume,String sex ) { this.nume = nume; this.sex=sex; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getNume() { return nume; } public void setNume(String nume) { this.nume = nume; } public boolean equals(Object obj){ if(obj instanceof Equals){ Equals e =(Equals)obj; if(e.getNume().equals(this.getNume())&&e.getSex().equals(this.getSex())){ return true; } } return false; } }
代码2;
public class equalsTest { public static void main(String[] args){ Equals e1 = new Equals("小白","18"); Equals e2 = new Equals("小白","18"); System.out.println(e1.equals(e2)); System.out.println("17".equals(17)); }}
输出结果图:
课堂笔记:
方法重载:
在同一个类中,方法名相同,参数列表必须不同,与反回值类型,访问修饰符无关,方法重写:方法名相同,参数列表相同,反回值类型必须一致或者是其子类和抽象类,访问权限不能于父类,不能抛出比父类更多的异常