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)
相關用法
- Java Field set()用法及代碼示例
- Java Field get()用法及代碼示例
- Java Field setFloat()用法及代碼示例
- Java Field getShort()用法及代碼示例
- Java Field setChar()用法及代碼示例
- Java Field isEnumConstant()用法及代碼示例
- Java Field getGenericType()用法及代碼示例
- Java Field getDouble()用法及代碼示例
- Java Field getBoolean()用法及代碼示例
- Java Field getByte()用法及代碼示例
- Java Field setLong()用法及代碼示例
- Java Field getChar()用法及代碼示例
- Java Field getDeclaringClass()用法及代碼示例
- Java Field getAnnotatedType()用法及代碼示例
- Java Field setByte()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Field equals() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。