日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

Java类类getDeclaredMethod()方法及示例

發(fā)布時間:2025/3/11 java 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java类类getDeclaredMethod()方法及示例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

類的類getDeclaredMethod()方法 (Class class getDeclaredMethod() method)

  • getDeclaredMethod() method is available in java.lang package.

    getDeclaredMethod()方法在java.lang包中可用。

  • getDeclaredMethod() method is used to return Method objects that indicate the given declared method of the class or an interface denoted by this Class object.

    getDeclaredMethod()方法用于返回Method對象,這些對象指示給定的類的聲明方法或由此Class對象表示的接口。

  • getDeclaredMethod() method is a non-static method, it is accessible with the class objects only and if we try to access the method with the class name then we will get an error.

    getDeclaredMethod()方法是一個非靜態(tài)方法,只能使用類對象進(jìn)行訪問,如果嘗試使用類名稱訪問該方法,則會收到錯誤消息。

  • getDeclaredMethod() method accepts two arguments, the first argument is of "String" type and the second argument is an array of "Class" type.

    getDeclaredMethod()方法接受兩個參數(shù),第一個參數(shù)為“字符串”類型,第二個參數(shù)為“類”類型的數(shù)組。

  • getDeclaredMethod() method may throw an exception at the time of returning a Method object.

    getDeclaredMethod()方法在返回Method對象時可能會引發(fā)異常。

    • NoSuchMethodException: In this exception when a specifying method does not exist.NoSuchMethodException :在此異常中,當(dāng)指定方法不存在時。
    • SecurityException: In this exception, it may raise when the security manager exists.SecurityException :在此異常中,當(dāng)安全管理器存在時可能會引發(fā)此異常。
    • NullPointerException: In this exception when the given Method name is null.NullPointerException :在此異常中,當(dāng)給定的Method名稱為null時。

Syntax:

句法:

public Method getDeclaredMethod (String method_name, Class ...paramType);

Parameter(s):

參數(shù):

  • String method_name – represents the name of the method.

    字符串method_name –表示方法的名稱。

  • Class ...paramType – represents the parameter array of "Class" type.

    Class ... paramType –表示“類”類型的參數(shù)數(shù)組。

Return value:

返回值:

The return type of this method is Method, it returns the Method object for the method of this Class meets the given method_name and parameter array paramType.

該方法的返回類型為Method ,它返回該Class方法滿足給定method_name和參數(shù)數(shù)組paramType的Method對象。

Example:

例:

// Java program to demonstrate the example of Method // getDeclaredMethod (String method_name, Class ...paramType) // method of Class import java.lang.reflect.*;public class GetDeclaredMethodOfClass {public static void main(String[] args) throws Exception {String str = new String();GetDeclaredMethodOfClass dc = new GetDeclaredMethodOfClass();// Get Class object of StringClass cl = str.getClass();// Get Class object of GetDeclaredMethodOfClassClass dm = dc.getClass();// Calling No argument MethodMethod no_argument_method = cl.getDeclaredMethod("length", null);System.out.println(" String Method = " + no_argument_method.toString());Class[] method_arguments = new Class[2];method_arguments[0] = Integer.class;method_arguments[1] = Float.class;// Calling argument MethodMethod argument_method = dm.getDeclaredMethod("argumentMethod", method_arguments);System.out.println("This Class Method = " + argument_method.toString());}public void argumentMethod(Integer i, Float f) {this.i = i;this.f = f;}public int i = 10;private float f = 10.2f; }

Output

輸出量

String Method = public int java.lang.String.length() This Class Method = public void GetDeclaredMethodOfClass.argumentMethod(java.lang.Integer,java.lang.Float)

翻譯自: https://www.includehelp.com/java/class-class-getdeclaredmethod-method-with-example.aspx

總結(jié)

以上是生活随笔為你收集整理的Java类类getDeclaredMethod()方法及示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。