当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Field equals()用法及代码示例


java.lang.reflect.Field的equals()方法用于比较两个字段对象。此方法比较两个字段对象,如果两个对象相等,则返回true,否则返回false。当且仅当当它们由相同的类声明并且具有相同的名称和类型时,才认为这两个Field对象相等。在调试对象属性(这实际上是Java中的类的字段)时,此方法非常有用。

用法:

public boolean equals(Object obj)

参数:此方法接受一个参数obj,它是要与该Field对象进行比较的参考对象。


返回值:如果两个对象相等,则此方法返回true,否则返回false。

以下示例程序旨在说明equals()方法:
示例1:

// Java program to demonstrate the above method 
  
import java.lang.reflect.Field; 
  
public class GFG { 
  
    public static void main(String[] args) 
        throws NoSuchFieldException, 
               SecurityException 
    { 
  
        // get the array of Field objects 
        Field[] fields 
            = User.class.getDeclaredFields(); 
        Field fieldObj 
            = User.class.getField("name"); 
  
        // print element of field array 
        // and compare it with fieldObj 
        for (int i = 0; i < fields.length; i++) { 
  
            // compare the fields with each other 
            boolean isEquals 
                = fields[i].equals(fieldObj); 
            if (isEquals) { 
                System.out.println( 
                    "Field -> ["
                    + fields[i] + "] and"
                    + " FieldObj -> ["
                    + fieldObj 
                    + "] are equal."); 
            } 
            else { 
                System.out.println( 
                    "Field -> ["
                    + fields[i] + "] and"
                    + " FieldObj -> ["
                    + fieldObj 
                    + "] are not equal."); 
            } 
        } 
    } 
} 
  
// User class 
class User { 
  
    public String name; 
    public int age; 
}
输出:

Field -> [public java.lang.String User.name]
and
FieldObj -> [public java.lang.String User.name]
are equal.

Field -> [public int User.age]
and
FieldObj -> [public java.lang.String User.name]
are not equal.

示例2:

// Java program to demonstrate the above method 
  
import java.lang.reflect.Field; 
  
public class GFG { 
  
    public static void main(String[] args) 
        throws NoSuchFieldException, 
               SecurityException 
    { 
  
        // get the array of Field objects 
        Field[] fields1 
            = Class.class.getDeclaredFields(); 
        Field[] fields2 
            = Class.class.getDeclaredFields(); 
  
        // print element of field array 1 and compare 
        // it with fields array 2 
        for (int i = 0; i < fields1.length; i++) { 
  
            for (int j = 0; j < fields2.length; j++) { 
  
                // compare the fields with each other 
                boolean isEquals 
                    = fields1[i].equals(fields2[j]); 
                if (isEquals) { 
                    System.out.println( 
                        "Field -> ["
                        + fields1[i] + "] and"
                        + " FieldObj -> ["
                        + fields2[j] 
                        + "] are equal."); 
                } 
                else { 
                    System.out.println( 
                        "Field -> ["
                        + fields1[i] + "] and"
                        + " FieldObj -> ["
                        + fields2[j] 
                        + "] are not equal."); 
                } 
            } 
        } 
    } 
} 
  
// Object of Class which contains 
// noOfStudents and studentNames 
class Class { 
  
    public int noOfStudents; 
    public String[] studentNames; 
}
输出:

Field -> [public int Class.noOfStudents]
and
FieldObj -> [public int Class.noOfStudents]
are equal.

Field -> [public int Class.noOfStudents]
and
FieldObj -> [public java.lang.String[] Class.studentNames]
are not equal.

Field -> [public java.lang.String[] Class.studentNames]
and
FieldObj -> [public int Class.noOfStudents]
are not equal.

Field -> [public java.lang.String[] Class.studentNames]
and
FieldObj -> [public java.lang.String[] Class.studentNames]
are equal.

参考文献: https://docs.oracle.com/javase/10/docs/api/java/lang/reflect/Field.html#equals(java.lang.Object)



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Field equals() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。