zoukankan      html  css  js  c++  java
  • equals()方法

    equals()方法是根类Object中的一个方法,子类可以根据需要重写该方法(比如:String类)。

    一、Object类中的equals()方法实现如下:

    1     public boolean equals(Object obj) {
    2         return (this == obj);
    3     }

    例子:

    复制代码
     1 package com.mianshi.easy;
     2 
     3 class A{}
     4 
     5 public class Equals {
     6 
     7     public static void main(String[] args) {
     8         A a1 = new A();
     9         A a2 = new A();
    10         A a3 = a2;
    11         System.out.println(a1.equals(a2));
    12         System.out.println(a3.equals(a2));
    13     }
    14 }
    15 
    16 结果:
    17 false
    18 true
    复制代码

    复制代码
     1 package com.mianshi.easy;
     2 
     3 class A{}
     4 
     5 public class Equals {
     6 
     7     public static void main(String[] args) {
     8         A a1 = new A();
     9         A a2 = new A();
    10         A a3 = a2;
    11         System.out.println(a1.equals(a2));
    12         System.out.println(a3.equals(a2));
    13     }
    14 }
    15 
    16 结果:
    17 false
    18 true
    复制代码

    类A默认继承自Object类,所以具有父类中的public修饰的的equals()方法,并且功能相同。

    该方法实现的功能是比较两个对象是否是同一个对象,是同一个对象回true,否则,返回false。equals()比较的是obj存放的对象的地址值,这一点跟“==”一样。

    二、String类的中的equals()方法实现如下:

    复制代码
     1  public boolean equals(Object anObject) {
     2         if (this == anObject) {              //先比较两个对象是否为同一个对象
     3             return true;
     4         }
     5         if (anObject instanceof String) {  
     6             String anotherString = (String)anObject;  
     7             int n = value.length;
     8             if (n == anotherString.value.length) {   //比较两个对象的值是否相同,值相同equals()返回true
     9                 char v1[] = value;
    10                 char v2[] = anotherString.value;
    11                 int i = 0;
    12                 while (n-- != 0) {
    13                     if (v1[i] != v2[i])
    14                         return false;
    15                     i++;
    16                 }
    17                 return true;
    18             }
    19         }
    20         return false;
    21     }
    复制代码

    例子:

    复制代码
     1 package com.mianshi.easy;
     2 
     3 public class Equals {
     4 
     5     public static void main(String[] args) {
     6         String a1 = new String("Hello");
     7         String a2 = new String("Hello");
     8         String a3 = "Hello";
     9         //a1和a2明显不是一个对象,但是equals()返回true
    10         System.out.println(a1.equals(a2));
    11         //a1和a3不是同一个对象,equals()返回true
    12         System.out.println(a3.equals(a1));
    13     }
    14 }
    15 
    16 结果:
    17 true
    18 true
    复制代码

    复制代码
     1 package com.mianshi.easy;
     2 
     3 public class Equals {
     4 
     5     public static void main(String[] args) {
     6         String a1 = new String("Hello");
     7         String a2 = new String("Hello");
     8         String a3 = "Hello";
     9         //a1和a2明显不是一个对象,但是equals()返回true
    10         System.out.println(a1.equals(a2));
    11         //a1和a3不是同一个对象,equals()返回true
    12         System.out.println(a3.equals(a1));
    13     }
    14 }
    15 
    16 结果:
    17 true
    18 true
    复制代码

    说明String类里面重写了父类Object类的equals()方法,重写后比较的是两个String对象的内容是否相同,相同则为true。

  • 相关阅读:
    sql语句开发使用---update
    获取弹出框的句柄,关闭弹出框
    水晶报表使用,解决相同数据库不同服务器使用同一个水晶报表模板问题?
    第一章 什么是SQL Server Integration Services (ssis) 系统。
    在 win 10 中使用sql 2012 附加低版本数据失败的解决办法。
    窗体间传值 委托应用
    有点小激动
    Adam
    SVN Unable to connect to a repository at URL问题解决
    C#基础---IComparable用法,实现List<T>.sort()排序
  • 原文地址:https://www.cnblogs.com/s844876674/p/4684537.html
Copyright © 2011-2022 走看看