java.lang.reflect.Method.getName() 方法示例



說明

java.lang.reflect.Method.getName() 方法以字串形式返回該方法的名稱。這是方法宣告類的二進位制名稱。

宣告

下面是 java.lang.reflect.Method.getName() 方法的宣告。

public String getName()

public String getName()

返回

底層成員的簡單名稱。

示例

package com.tutorialspoint;

import java.lang.reflect.Method;

public class MethodDemo {

   public static void main(String[] args) {

      Method[] methods = SampleClass.class.getMethods();
      for (Method method : methods) {
         System.out.println("Method: " + method.getName());
      }
   }
}

class SampleClass {
   private String sampleField;

   public String getSampleField() {
      return sampleField;
   }

   public void setSampleField(String sampleField) {
      this.sampleField = sampleField; 
   } 
}

現場演示

Method: getSampleField
Method: setSampleField
Method: wait
Method: wait
Method: wait
Method: equals
Method: toString
Method: hashCode
Method: getClass
Method: notify
Method: notifyAll
讓我們編譯並執行上面的程式,它將產生以下結果 -
列印頁面
© . All rights reserved.