構造函數類提供有關一個類的單個構造函數的信息,並且還提供對那個構造函數的訪問。
java.lang.reflect.Constructor的equals()方法用於將此Constructor與傳遞的對象進行比較。如果對象相同,則該方法返回true。在Java中,當且僅當兩個Constructor對象由相同的類聲明且具有相同的形式參數類型時,它們才是相同的。
用法:
public boolean equals(Object obj)
參數:此方法接受obj,它是要與之進行比較的參考對象。
返回:如果此對象與obj參數相同,則此方法返回true。否則返回false。
以下示例程序旨在說明equals()方法:
示例1:
// Java program to illustrate equals() method
import java.lang.reflect.Constructor;
public class GFG {
public static void main(String[] args)
{
// create a class object
Class classObj = String.class;
// get Constructor object array from class object
Constructor[] cons = classObj.getConstructors();
// apply equals method
System.out.println("Constructor 1: " + cons[0]);
System.out.println("Constructor 2: " + cons[1]);
boolean result = cons[0].equals(cons[1]);
// print result
System.out.println("Both constructor are equal: "
+ result);
}
}
輸出:
Constructor 1: public java.lang.String(byte[], int, int) Constructor 2: public java.lang.String(byte[], java.nio.charset.Charset) Both constructor are equal: false
示例2:
// Java program to illustrate equals() method
import java.lang.reflect.Constructor;
import java.util.ArrayList;
public class GFG {
public static void main(String[] args)
{
// create a class object
Class classObj = ArrayList.class;
// get Constructor object array from class object
Constructor[] cons = classObj.getConstructors();
// apply equals method
System.out.println("Constructor 1: " + cons[0]);
Object obj = cons[0];
System.out.println("Object: " + obj);
boolean result = cons[0].equals(obj);
// print result
System.out.println("Both constructor are equal: "
+ result);
}
}
輸出:
Constructor 1: public java.util.ArrayList(java.util.Collection) Object: public java.util.ArrayList(java.util.Collection) Both constructor are equal: true
相關用法
- Java Constructor getName()用法及代碼示例
- Java Constructor toGenericString()用法及代碼示例
- Java Constructor getGenericExceptionTypes()用法及代碼示例
- Java Constructor getDeclaringClass()用法及代碼示例
- Java Constructor getAnnotatedReturnType()用法及代碼示例
- Java Constructor getParameterAnnotations()用法及代碼示例
- Java Constructor getAnnotation()用法及代碼示例
- Java Constructor getAnnotatedReceiverType()用法及代碼示例
- Java Constructor getDeclaredAnnotations()用法及代碼示例
- Java Constructor toString()用法及代碼示例
- Java Constructor isVarArgs()用法及代碼示例
- Java Constructor isSynthetic()用法及代碼示例
- Java Constructor hashCode()用法及代碼示例
- Java Constructor getParameterCount()用法及代碼示例
- Java Constructor getGenericParameterTypes()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Constructor equals() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。