首页 > 代码库 > equals跟向上,向下转型

equals跟向上,向下转型

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;
	}
	
	
}

  

equals跟向上,向下转型