當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Method類 equals()用法及代碼示例


Method類的java.lang.reflect.Method.equals(Object obj)方法將此Method Object與指定對象進行比較,以作為equal(object obj)方法的參數。如果Method對象與傳遞的對象相同,則此方法返回true。如果兩個方法是由相同的類聲明的,並且具有相同的名稱,形式參數類型和返回類型,則它們是相同的。

用法:

public boolean equals(Object obj)

參數:此方法接受強製參數obj,它是要比較的對象。



返回值:如果Method對象與作為參數傳遞的對象相同,則該方法返回true,否則返回false。

以下示例程序旨在說明Method類的equals(Object obj)方法:

範例1:當兩個對象相同時。

/* 
* Program Demonstrate equals(Object Obj) 
* method of Method Class. 
*/
import java.lang.reflect.Method; 
  
public class GFG { 
  
    public static void main(String[] args) 
        throws NoSuchMethodException, 
               SecurityException, 
               ClassNotFoundException 
    { 
  
        // Get all method of class GFGClass 
        // and store in an Array of Method Objects 
        Method[] methods = GFGClass.class.getMethods(); 
  
        // create a method object by passing 
        // method name and parameter type 
        Method method = GFGClass.class
                            .getMethod("add", String.class); 
  
        System.out.println("First Method object from array "
                           + "create by getMethods():"); 
        System.out.println(methods[0]); 
        System.out.println("Method object created by "
                           + "getMethod():"); 
        System.out.println(method); 
  
        // compare both objects by equals method 
        if (methods[0].equals(method)) { 
            System.out.println("Both Method objects"
                               + " are equal"); 
        } 
        else { 
            System.out.println("Both Method objects"
                               + " are not equal"); 
        } 
    } 
} 
  
// This is Sample Class for which 
// the class object are created 
class GFGClass { 
  
    // We have two variables in this class 
  
    private String field1; 
  
    public GFGClass() 
    { 
    } 
  
    // creatin methods 
    public String add(String field2) 
    { 
        return this.field1 + field2; 
    } 
}
輸出:
First Method object from array create by getMethods():
public java.lang.String GFGClass.add(java.lang.String)
Method object created by getMethod():
public java.lang.String GFGClass.add(java.lang.String)
Both Method objects are equal

範例2:當兩個對象都不相同時。

/* 
* Program Demonstrate equals(Object Obj) method of Method Class. 
*/
import java.lang.reflect.Method; 
  
// This is Main Class 
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Get all method of class GFGClass 
        // and store as Array of Method Objects 
        Method[] methods = GFGClass.class.getMethods(); 
  
        // select any two method and compare 
        System.out.println("Methods are:"); 
        System.out.println(methods[0]); 
        System.out.println(methods[1]); 
        if (methods[0].equals(methods[1])) { 
            System.out.println("Methods are equal"); 
        } 
        else { 
            System.out.println("Methods are not equal"); 
        } 
    } 
} 
  
// This is Sample Class 
class GFGClass { 
  
    // We have two variables in this class 
    private String field1; 
    private String field2; 
  
    public GFGClass() 
    { 
    } 
  
    // we have four methods in this class 
    public String getField1() 
    { 
        return field1; 
    } 
  
    public void setField1(String field1) 
    { 
        this.field1 = field1; 
    } 
  
    public String getField2() 
    { 
        return field2; 
    } 
  
    public void setField2(String field2) 
    { 
        this.field2 = field2; 
    } 
}
輸出:
Methods are:
public java.lang.String GFGClass.getField1()
public void GFGClass.setField1(java.lang.String)
Methods are not equal

參考:
https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#equals-java.lang.Object-




相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Method Class | equals() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。