现在的位置: 首页 > 综合 > 正文

字符串(找工作经常被问到的题目) 十

2018年04月10日 ⁄ 综合 ⁄ 共 948字 ⁄ 字号 评论关闭

因为在面试中 equals方法是什么意思

对于Object中equals方法来说 他是判断二个对象地址是否相等

对于继承了Object中得其他类的 并且重写了equals方法

判断内容一致

如果没有重写 只是继承  还是判断二个对象地址是否一致

public class EqualsTest
{
               public static void main(String[] args)
               {
                     Student stu1 = new Student("zhangsan");
                     Student stu2 = new Student("zhangsan");
  
                   System.out.println(stu1.equals(stu2));
                  System.out.println(stu1==stu2);   
              }
}
class Student 

{
                              String name;
          public Student(String name)
          {
             this.name = name;
          }
 
               public boolean equals(Object anObject)
         {
                  if(this == anObject)
                   {                                                                //输出一个假 一个真
                return true;
                   }
                     if(anObject instanceof Student)
  {
   Student student = (Student)anObject;
   if (student.name.equals(this.name))

{

return true ;

}

retun false;}

抱歉!评论已关闭.