`

Simple of Java Reflection

    博客分类:
  • Java
阅读更多
define an entityObject:
java 代码
  1. class Employee {   
  2.     // 定义一个员工类   
  3.     public Employee() {   
  4.         age = 10;   
  5.         name = null;   
  6.     }   
  7.     // 将要被调用的方法   
  8.     public void setAge(int a) {   
  9.         age = a;   
  10.     }   
  11.     // 将要被调用的方法   
  12.     public int getAge() {   
  13.         return age;   
  14.     }   
  15.     // 将要被调用的方法   
  16.         public void printInfo(String name,int age){   
  17.         System.out.println("myName is:"+name+"  myAge is:"+age);   
  18.     }   
  19.     private int age;   
  20.     private String name;   
  21. }  

Test  Codes:

java 代码
  1. class ReflectTest{   
  2.     public static void main(String[] args){   
  3.         Employee emp=new Employee();   
  4.         Class c=emp.getClass();   
  5.         try{   
  6.             Method printInfo=c.getMethod("printInfo"new Class[]{String.class,int.class});   
  7.             Object[] args1=new Object[]{"zhuhaihua",23};   
  8.             printInfo.invoke(emp, args1);   
  9.         }catch(Exception e){   
  10.         }   
  11.     }   
  12. }  

just copy two pieces of code into the IDE(just like Eclipse) then run the ReflectTest class,you will get the result:
    myName is:zhuhaihua  myAge is:23

分享到:
评论

相关推荐

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

    A Short History of Java 9 Common Misconceptions about Java 11 Chapter 2: The Java Programming Environment 15 Installing the Java Development Kit 16 Choosing a Development Environment 21 Using ...

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

    This revised edition of the classic Core Java™, Volume I–Fundamentals, is the definitive guide to Java for serious programmers who want to put Java to work on real projects. Fully updated for the ...

    Java Interview Guide.epub

    I've conducted over 100 interviews at Fortune 500 companies and meticulously collected the most effective questions with simple, straightforward explanations. This book will teach you object-...

    java高手必备知识点

    GoF(Gang of Four)设计模式包括创建型、结构型和行为型三种类型,如单例模式、工厂模式、策略模式等。J2EE设计模式则针对企业级应用开发中常见的问题提供解决方案,例如MVC(模型-视图-控制器)、业务代表模式等。...

    The.Go.Programming.Language.0134190440.epub

    The first chapter is a tutorial on the basic concepts of Go, introduced through programs for file I/O and text processing, simple graphics, and web clients and servers. Early chapters cover the ...

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

    超级有影响力的Java面试题大全文档 1.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面。抽象并不打算了解全部问题,而只是选择其中的一部分,暂时不用部分细节。...

    Clojure.High.Performance.JVM.Programming.epub

    Write simple multicore programs using Clojure's core concepts, such as atoms, agents, and refs Get to grips with Clojure's concurrency and state-management primitives in depth Analyze latency using ...

    Android代码-javalin

    Javalin - A simple web framework for Java and Kotlin Javalin is a very lightweight web framework for Kotlin and Java which supports WebSockets, HTTP2 and async requests. Javalin’s main goals are ...

    Professional C# 3rd Edition

    The Relationship of C# to .NET 4 The Common Language Runtime 4 Advantages of Managed Code 4 A Closer Look at Intermediate Language 7 Support for Object Orientation and Interfaces 8 Distinct Value and ...

    二十三种设计模式【PDF版】

    《Thingking in Java》(第一版中文)是这样描述设计模式的:他在由 Gamma, Helm 和 Johnson Vlissides 简称 Gang of Four(四人 帮),缩写 GoF 编著的《Design Patterns》一书中被定义成一个“里程碑”。事实上,那本书...

    ZendFramework中文文档

    9.4.6. Working with Fractions of Seconds 9.4.7. Sunrise / Sunset 9.5. Creation of dates 9.5.1. Create the actual date 9.5.2. Create a date from database 9.5.3. Create dates from an array 9.6. ...

Global site tag (gtag.js) - Google Analytics