package day14; public class instanceof1 { public static void main(String[] args) { teacher1 t1=new teacher1(); teacher1 t2=new teacher1(); t1.setAge("李四"); t2.setAge("李四"); System.out.println(t1.equals(t2)); } } class teacher1{ private String name;//要是引用类型 public boolean equals(Object obj) {//这里进行了向上转型 object pbj=new teacher1; if(obj instanceof teacher1){ teacher1 t1=(teacher1)obj; if(this.name.equals(t1.name)){ return true; }else{ return false; } }else{ return false; } }; public String getAge() { return name; } public void setAge(String name) { this.name = name; } }
时间: 2024-10-14 14:10:26