`
yeelor
  • 浏览: 409156 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Java Reflection:Methods类方法

    博客分类:
  • Java
 
阅读更多

用Class 的getMethods 可以取的类的全部方法的定义

 

Class aClass = ...//obtain class object  
Method[] methods = aClass.getMethods(); 

 

如果知道类方法的参数类型,则可以使用getMethod 取的对应的方法定义,如:

Class  aClass = ...//obtain class object  
Method method = aClass.getMethod("doSomething", new Class[]{String.class}); 

 

 

 

上例取的方法名为doSomething 带一个String为参数的方法定义。如果找不到对应的方法,JavaReflection 抛出NoSuchMethodException 异常。

如果需要方法的方法不带参数,则使用null 做为getMethod的第二个参数,如下:

 

Class  aClass = ...//obtain class object  
Method method = aClass.getMethod("doSomething", null); 

 


 1. 获取方法的参数和返回值定义

获取参数定义

 

Method method = ... // obtain method - see above  
Class[] parameterTypes = method.getParameterTypes(); 

 

 

获取返回值定义

 

Method method = ... // obtain method - see above
Class returnType = method.getReturnType();

 
2. 使用Method对象调用该方法

 

//get method that takes a String as argument  
MyObject o = new MyObject();
Method method = MyObject.class.getMethod("doSomething", String.class); 
Object returnValue = method.invoke(o, "parameter-value1"); 
//method.invoke 的调用个参数为该方法对应的类对象,如果方法为static 类型,则使用null,后面的参数为该方法的参数值。
Method method = MyObject.class.getMethod("doSomething", String.class); 
Object returnValue = method.invoke(null, "parameter-value1"); 

 

 

分享到:
评论

相关推荐

    侯捷讲Java的reflection机制

    Reflection 是Java 被視為動態(或準動態)語言的㆒個關鍵性質。這個機制允許程 式在執行期透過Reflection APIs 取得任何㆒個已知名稱的class 的內部資訊,包括 其modifiers(諸如public, static 等等)、superclass...

    Java2核心技术卷I+卷2:基础知识(第8版) 代码

    Java 核心技术 卷1 Index ...Calling Java Methods 956 Accessing Array Elements 962 Handling Errors 966 Using the Invocation API 970 A Complete Example: Accessing the Windows Registry 975 Index 991

    java反射机制 读者基础:具备Java 语言基础

    这个机制允许程序在运行时透过Reflection APIs取得任何一个已知名称的class的内部信息,包括其modifiers(诸如public,static 等等)、superclass(例如Object)、实现之interfaces(例如Cloneable),也包括fields和...

    java7帮助文档

    Preferences API Ref Objects Reflection Regular Expressions Versioning Zip Instrumentation Java Virtual Machine Java HotSpot Client and Server VM Description of Java Conceptual Diagram What's ...

    java反射原理详解

    很好的有百科名片 JAVA反射机制:“程序运行时,允许改变...换句话说,Java程序可以加载一个运行时才得知名称的class,获悉其完整构造(但不包括methods定义),并生成其对象实体、或对其fields设值、或唤起其methods。

    候捷谈Java反射机制

    JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。...

    java 面试题 总结

    声明方法的存在而不去实现它的类被叫做抽象类(abstract class),它用于要创建一个体现某些基本行为的类,并为该类声明方法,但不能在该类中实现该类的情况。不能创建abstract 类的实例。然而可以创建一个变量,其...

    java反射机制

    JAVA反射机制:“程序运行时,允许改变程序结构或变量...换句话说,Java程序可以加载一个运行时才得知名称的class,获悉其完整构造(但不包括methods定义),并生成其对象实体、或对其fields设值、或唤起其methods。

    Effective Java 3rd edition(Effective Java第三版英文原版)附第二版

    Item 85: Prefer alternatives to Java serialization Item 86: Implement Serializable with great caution Item 87: Consider using a custom serialized form Item 88: Write readObject methods defensively ...

    java反射机制介绍

    换句话说,Java程序可以加载一个运行时才得知名称的class,获悉其完整构造(但不包括methods定义),并生成其对象实体、或对其fields设值、或唤起其methods1。这种“看透class”的能力(the ability of the program ...

    Core Java, Volume II--Advanced Features (9th Edition).pdf

    For detailed coverage of fundamental Java SE 7 features, including objects, classes, inheritance, interfaces, reflection, events, exceptions, graphics, Swing, generics, collections, concurrency, and ...

    Learn.Java.for.Android.Development_Apress.2010+src

    What you''ll learn The Java language: This book provides complete coverage of nearly every pre-Java version 7 language feature (native methods are briefly mentioned but not formally covered)....

    Core Java(Volume II--Advanced Features 9th Edition)

    For detailed coverage of fundamental Java SE 7 features, including objects, classes, inheritance, interfaces, reflection, events, exceptions, graphics, Swing, generics, collections, concurrency, and ...

    advanced java.pdf

    1、How to create and destroy objects 2、Using methods common to ...11、How to use Reflection effectively 12、Dynamic languages support 13、Java Compiler API 14、Java Annotation Processors 15、Java Agents

    Android代码-symbols

    In some cases, in Java (ex: when using reflection methods) we need to refer to class attributes. To do so we usually create static strings of the names of the attributes. This is boiler-plate code and...

    java.核心技术.第八版 卷1

    java 核心技术 第八版 core 卷1 Core Java, 8th Edition Core Java. Volume I. Fundamentals, 8th Edition Core Java. Volume II. Advanced Features, 8th Edition 官方网站 http://horstmann.com/corejava.html ...

    超级有影响力霸气的Java面试题大全文档

     声明方法的存在而不去实现它的类被叫做抽象类(abstract class),它用于要创建一个体现某些基本行为的类,并为该类声明方法,但不能在该类中实现该类的情况。不能创建abstract 类的实例。然而可以创建一个变量,...

    Java8Tests:Java7 8 新特性

    Java8TestsJava7 8 新特性Java8 新特性Repeating AnnotationsParameter ReflectionType AnnotationDefault methodsMethod referencesLambda Expressions相关文章

Global site tag (gtag.js) - Google Analytics