Application of Reflection in Java

Source: Internet
Author: User

  

Class b{
public static void Main (string[] arg) {
Class c_a = class.forname (PackageName + "." + "a");
Object obj_a = C_a.newinstance ();
Method do_m = C_a.getdeclaredmethod ("Do", New class[]{});
Prepare data for the Mathod
Do_m.invoke (obj_a, New object[]{});
}
}

Originally felt very troublesome reflection, in fact, look carefully, think, in fact, reflection is very natural. Fortunately, in other C + + code, if you need to call a DLL, you need to declare the calling method before you call it. Reflection is similar to this case. Declare a method first

Method m = C_a.getdeclaredmethod ("Do", New class[]{});

and then call

m.invoke(obj_a, newObject[]{});

Reflection is very simple to use, but it can solve a lot of problems, it is a good tool to say. Although the code seems to be more, but under the thought, in fact, it is also very natural things, do not need to try to remember anything.

Import Java.lang.reflect.Method;

public class Invokemethods {
public static void Main (string[] args) {
Employee EMP = new Employee ();
Class CL = Emp.getclass ();//Is class, not class
/getclass gets the object of the type to which the EMP object belongs, class is classes
/class is a class specifically used to describe classes, such as those that describe a class with fields,
/methods, constructors, etc.!
try {
/getmethod method The first parameter specifies a method name that needs to be called
/Here is the Setage method for the employee class, and the second argument is to call the
A list of parameter types for the method, which is the parameter type! Null if no parameter can be specified
/The method returns a Method object
Method SAge = Cl.getmethod ("Setage", new class[] {int.class});/Parameters must be the same as in the method int and integer,double and double are treated as different types
Method GAge = Cl.getmethod ("Getage", null);
Method pName = Cl.getmethod ("Printname",
New class[] {string.class});

Object[] args1 = {new Integer (25)};
Parameter list
An EMP is an implicit parameter that the method is not a static method must specify
Sage.invoke (EMP, ARGS1);
Integer age = (integer) gage.invoke (emp, NULL);
int age = Age.intvalue ();
System.out.println ("The Employee Age is:" + age);
Object[] Args3 = {new String ("Jack")};
Pname.invoke (EMP, ARGS3);
} catch (Exception e) {
E.printstacktrace ();
}
System.exit (0);
}
}

Class Employee {
Define an employee class
Public Employee () {
Age = 0;
name = NULL;
}
The method that will be called
public void Setage (int a) {
age = A;
}
The method that will be called
public int getage () {
return age;
}
The method that will be called
public void Printname (String N) {
name = N;
System.out.println ("The Employee name is:" + name);
}
private int age;
private String name;
}

Application of Reflection in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.