`
AquariusM
  • 浏览: 145921 次
  • 性别: Icon_minigender_1
  • 来自: 南阳
社区版块
存档分类
最新评论

2010年9月25号---Java基础学习之---Java反射方法学习getMethod()和invoke()方法学习

阅读更多

 

getMethod

public Method getMethod(String name,
                        Class... parameterTypes)
                 throws NoSuchMethodException,
                        SecurityException

Returns a Method object that reflects the specified public member method of the class or interface represented by thisClass object. The name parameter is a String specifying the simple name the desired method. The parameterTypes parameter is an array of Class objects that identify the method's formal parameter types, in declared order. If parameterTypes isnull, it is treated as if it were an empty array.

If the name is "<init>"or "<clinit>" a NoSuchMethodException is raised. Otherwise, the method to be reflected is determined by the algorithm that follows. Let C be the class represented by this object:

返回一个反射的public方法,该方法是由类对象确定的一个类或者是借口。name参数是一个String类型制定一个simple name指向上边的方法。

parameterTypes参数是一个class对象的数组,该数组指定该方法的参数类型。如果parameterTypes参数是null,那么它就被作为一个空数组看待。

  1. C is searched for any matching methods. If no matching method is found, the algorithm of step 1 is invoked recursively on the superclass of C.
  2. If no method was found in step 1 above, the superinterfaces of C are searched for a matching method. If any such method is found, it is reflected.
To find a matching method in a class C:  If C declares exactly one public method with the specified name and exactly the same formal parameter types, that is the method reflected. If more than one such method is found in C, and one of these methods has a return type that is more specific than any of the others, that method is reflected; otherwise one of the methods is chosen arbitrarily.

See The Java Language Specification, sections 8.2 and 8.4.

 

Parameters:
name - the name of the method
parameterTypes - the list of parameters
Returns:
the Method object that matches the specified name and parameterTypes
Throws:
NoSuchMethodException - if a matching method is not found or if the name is "<init>"or "<clinit>".
NullPointerException - if name is null
SecurityException - If a security manager, s, is present and any of the following conditions is met:
Since:
JDK1.1

invoke
public Object invoke(Object obj,
                     Object[] args)
              throws IllegalAccessException,
                     IllegalArgumentException,
                     InvocationTargetException
Invokes the underlying method represented by this Method object, on the specified object with the specified parameters. Individual parameters are automatically unwrapped to match primitive formal parameters, and both primitive and reference parameters are subject to method invocation conversions as necessary.
调用由这个Method对象代表的基本方法,用指定的参数在指定的对象上调用。个别的参数是自动打开来符合基本数据类型的参数,并且不管基本数据类型还是引用数据类型的参数都是方法调用这都取决于方法调用中的转换需求。

If the underlying method is static, then the specified obj argument is ignored. It may be null.

If the number of formal parameters required by the underlying method is 0, the supplied args array may be of length 0 or null.

If the underlying method is an instance method, it is invoked using dynamic method lookup as documented in The Java Language Specification, Second Edition, section 15.12.4.4; in particular, overriding based on the runtime type of the target object will occur.

如果基本的方法时静态方法,那么指定的obj参数就被忽略。它可以是null。。。

If the underlying method is static, the class that declared the method is initialized if it has not already been initialized.

If the method completes normally, the value it returns is returned to the caller of invoke; if the value has a primitive type, it is first appropriately wrapped in an object. If the underlying method return type is void, the invocation returns null.

 

Parameters:
obj - the object the underlying method is invoked from
args - the arguments used for the method call
Returns:
the result of dispatching the method represented by this object on obj with parameters args
未完待续。。。

 

分享到:
评论

相关推荐

    Java方法反射调用demo

    Java反射是Java编程语言中的一个强大特性,它允许在运行时检查类、接口、字段和方法的信息,并且能够在运行时动态地创建对象和调用方法。这个特性使得Java具有了高度的灵活性,常用于框架开发、插件系统、元编程等...

    Java反射机制的使用和学习方法

    Java反射机制是Java编程语言中的一个强大特性,它允许程序在运行时检查和操作类、接口、对象等的内部信息。这一机制对于理解和实现高级框架、动态代理、元数据处理等场景至关重要。以下是对Java反射机制的详细说明:...

    Java反射方法调用

    Java反射机制是Java语言提供的一种强大的工具,它允许我们在运行时检查类、接口、字段和方法的信息,并且能够在运行时动态地创建对象和调用方法。这个特性在处理不确定类型的对象或者实现动态代理等场景中非常有用。...

    java面试题--反射机制

    动态加载类是Java反射机制的重要应用场景之一。通过`Class.forName()`方法或者`ClassLoader`的`loadClass()`方法,可以根据类名字符串在运行时加载类。 #### 七、操作成员 - **创建对象**:使用`Class`对象的`new...

    实战java反射机制-让你迅速认识java强大的反射机制

    Java反射机制是Java编程语言中的一个强大特性,它允许程序在运行时检查和操作类、接口、字段和方法的信息,甚至动态地创建对象并调用其方法。通过反射,开发者可以实现高度灵活和动态的代码,这对于框架开发、元编程...

    【IT十八掌徐培成】Java基础第25天-01.反射基础.zip

    徐培成老师的课程将会详细讲解这些概念,并通过实例演示如何使用反射,帮助学习者理解反射的原理和应用,进一步提升Java编程能力。观看“Java基础第25天-01.反射基础.avi”视频,你将能够掌握如何在实践中灵活运用...

    JAVA基础-反射-枚举

    ### JAVA基础-反射-枚举知识点详解 #### 一、反射概述 反射是Java语言提供的一种能在运行时分析类和对象的能力。通过反射,我们可以在程序运行时动态地获取类的信息(如类名、方法、构造函数等)并操作这些信息。 ...

    java反射机制及Method.invoke解释

    Java 反射机制及 Method.invoke 解释 Java 反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法;这种动态获取的信息以及动态调用对象的...

    java 反射得到某个方法

    在Java编程语言中,反射(Reflection)是一种强大的工具,它允许程序在运行时检查和操作类、接口、字段以及方法等对象。通过反射,我们可以在不知道具体类名或方法名的情况下,动态地调用对象的方法或访问其属性。在...

    2020老杜最新Java零基础进阶视频教程-反射机制课件

    4. Java反射中的主要类和方法 - `Class`: 提供了获取类信息的各种方法,如`getConstructors()`获取构造方法,`getMethods()`获取所有的公共方法,`getFields()`获取公共字段等。 - `Constructor`: 表示类的构造方法...

    Java 反射创建get set方法及反射方法的调用

    ### Java反射创建get set方法及反射方法的调用 #### 概述 在Java编程语言中,反射(Reflection)是一种强大的技术,它允许程序在运行时检查类、接口、字段和方法的信息,并能够动态地创建对象和调用方法。本文将...

    Java反射机制学习(二)

    在Java编程语言中,反射机制是一项强大的工具,它允许程序在运行时检查并操作类、接口、字段和方法的信息。这篇博文"Java反射机制学习(二)"可能深入探讨了如何利用反射进行动态类型处理、访问私有成员以及创建对象...

    java反射 java反射 java反射java反射

    在Java中,反射机制提供了强大的能力,包括在运行时检查类的结构、创建对象实例、调用方法以及访问和修改字段值。本文将深入探讨Java反射的相关知识点。 1. 常用`Class`类方法: - `Class.forName(String ...

    JAVA基础--JAVA中的反射机制详解

    JAVA 反射机制是 Java 语言中的一种动态获取信息和动态调用对象方法的功能。它允许程序在运行时获取类的信息、构造对象、获取成员变量和方法、调用对象的方法等。 Java 反射机制主要提供了以下功能: 1. 在运行时...

    Java反射调用方法

    Java反射是Java语言提供的一种强大的动态类型特性,它允许程序在运行时检查和操作类、接口、对象等的内部信息,包括但不限于获取类的方法、字段、构造器等,并能动态调用方法和修改字段值。这个特性使得Java具有了更...

    反射实例-JAVA反射机制

    在Java反射中,针对类的不同组成部分(构造函数、字段和方法),`java.lang.Class`类提供了多种反射调用方式来获取信息。以下是几种常用的反射调用: - **获取构造函数**:`Constructor getConstructor(Class[] ...

    java反射机制,调用私有方法

    Java反射机制是Java编程语言中的一个强大工具,它允许程序在运行时检查和操作类、接口、字段和方法的信息。这种动态类型的能力使得Java代码能够处理未知或未提前定义的对象,增强了程序的灵活性和可扩展性。在Java中...

    Invoke反射

    反射允许程序在运行时检查自身的信息,并且能够动态地创建对象、调用方法、访问属性和字段,以及执行其他与类型相关的操作。 在.NET中,每个类型都包含有关其成员(如方法、属性和事件)的信息。当你有一个类型的...

    java反射,获取所有属性、方法以及List集合类

    在Java中,反射主要用于在运行时分析类和对象,包括访问私有成员、调用私有方法、创建对象、获取类信息等。本篇文章将深入探讨如何使用Java反射来获取一个类的所有属性、方法,并处理List集合类。 首先,让我们了解...

    Java基础之-反射(非常重要)

    Java反射是Java编程语言中的一个强大工具,它允许程序在运行时检查类、接口、字段和方法的信息,并能动态地创建对象和调用方法。在深入理解Java反射之前,我们首先要明白面向对象的基本概念,包括类、对象、方法以及...

Global site tag (gtag.js) - Google Analytics