package com.saly.bean;
public class Person {
private String name;
private int age;
public Person() {
super();
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public static void show(){
System.out.println("person static show run");
}
public void function(String a,int b){
System.out.println("person param function"+"a="+a+"b="+b);
}
private void privateShow(){
System.out.println("private show run");
}
@Override
public String toString() {
return "Person [age=" + age + ", name=" + name + "]";
}
}
package com.saly.test;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/* *
* 反射机制是再运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法,对于任意一个对象,
* 都能够调用它的任意一个方法和属性,这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。
* @author saly
*
*/
public class Reflect {
public static void main(String[] args) throws Exception {
String className = "com.saly.bean.Person";
Class<?> clazz = Class.forName(className);
//accessStaticMethed(args, clazz);
accessParamMethed(clazz);
}
/**
* 访问带参数的方法
* @param clazz
* @throws NoSuchMethodException
* @throws InstantiationException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
private static void accessParamMethed(Class<?> clazz)
throws NoSuchMethodException, InstantiationException,
IllegalAccessException, InvocationTargetException {
Method method = clazz.getMethod("function",String.class,int.class);
//创建实例对象
Object obj = clazz.newInstance();
method.invoke(obj,"salyfan",25);
}
/**
* 访问静态方法
* 不需要创建对象
* @param args
* @param clazz
* @return
* @throws NoSuchMethodException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
private static void accessStaticMethed(String[] args, Class<?> clazz)
throws NoSuchMethodException, IllegalAccessException,
InvocationTargetException {
Method method = clazz.getMethod("show", null);
method.invoke(null, null);
}
/**
* 反射技术(访问空参数的函数)
* 需要创建对象
* @param args
* @param clazz
* @throws NoSuchMethodException
* @throws InstantiationException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
private static void accessMethed(String[] args, Class<?> clazz)
throws NoSuchMethodException, InstantiationException,
IllegalAccessException, InvocationTargetException {
//获取所有公有方法(包括继承父类的)
Method[] meth = clazz.getMethods();
for(Method m:meth){
//System.out.println(m);
}
System.out.println("................................");
//获取所有声明的方法(包括私有,但不包括继承父类的)
Method[] methods = clazz.getDeclaredMethods();
for(Method m:methods){
//System.out.println(m);
}
Method method = clazz.getMethod("toString", null);
//创建实例对象
Object obj = clazz.newInstance();
Object returnValue = method.invoke(obj, args);
System.out.println(returnValue);
}
/**
* 访问字段
* @param clazz
* @throws NoSuchFieldException
* @throws InstantiationException
* @throws IllegalAccessException
*/
private static void accessField(Class<?> clazz)
throws NoSuchFieldException, InstantiationException,
IllegalAccessException {
//获取该类的名称为age的字段()
Field ageField = clazz.getDeclaredField("age");
System.out.println(ageField);
//创建实例对象
Object obj = clazz.newInstance();
//访问私有对象
//对该私有字段进行取消权限检查的能力
ageField.setAccessible(true);//暴力访问
ageField.set(obj, 25);
Object o = ageField.get(obj);
System.out.println(o);
}
}
public class Person {
private String name;
private int age;
public Person() {
super();
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public static void show(){
System.out.println("person static show run");
}
public void function(String a,int b){
System.out.println("person param function"+"a="+a+"b="+b);
}
private void privateShow(){
System.out.println("private show run");
}
@Override
public String toString() {
return "Person [age=" + age + ", name=" + name + "]";
}
}
package com.saly.test;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/* *
* 反射机制是再运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法,对于任意一个对象,
* 都能够调用它的任意一个方法和属性,这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。
* @author saly
*
*/
public class Reflect {
public static void main(String[] args) throws Exception {
String className = "com.saly.bean.Person";
Class<?> clazz = Class.forName(className);
//accessStaticMethed(args, clazz);
accessParamMethed(clazz);
}
/**
* 访问带参数的方法
* @param clazz
* @throws NoSuchMethodException
* @throws InstantiationException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
private static void accessParamMethed(Class<?> clazz)
throws NoSuchMethodException, InstantiationException,
IllegalAccessException, InvocationTargetException {
Method method = clazz.getMethod("function",String.class,int.class);
//创建实例对象
Object obj = clazz.newInstance();
method.invoke(obj,"salyfan",25);
}
/**
* 访问静态方法
* 不需要创建对象
* @param args
* @param clazz
* @return
* @throws NoSuchMethodException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
private static void accessStaticMethed(String[] args, Class<?> clazz)
throws NoSuchMethodException, IllegalAccessException,
InvocationTargetException {
Method method = clazz.getMethod("show", null);
method.invoke(null, null);
}
/**
* 反射技术(访问空参数的函数)
* 需要创建对象
* @param args
* @param clazz
* @throws NoSuchMethodException
* @throws InstantiationException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
private static void accessMethed(String[] args, Class<?> clazz)
throws NoSuchMethodException, InstantiationException,
IllegalAccessException, InvocationTargetException {
//获取所有公有方法(包括继承父类的)
Method[] meth = clazz.getMethods();
for(Method m:meth){
//System.out.println(m);
}
System.out.println("................................");
//获取所有声明的方法(包括私有,但不包括继承父类的)
Method[] methods = clazz.getDeclaredMethods();
for(Method m:methods){
//System.out.println(m);
}
Method method = clazz.getMethod("toString", null);
//创建实例对象
Object obj = clazz.newInstance();
Object returnValue = method.invoke(obj, args);
System.out.println(returnValue);
}
/**
* 访问字段
* @param clazz
* @throws NoSuchFieldException
* @throws InstantiationException
* @throws IllegalAccessException
*/
private static void accessField(Class<?> clazz)
throws NoSuchFieldException, InstantiationException,
IllegalAccessException {
//获取该类的名称为age的字段()
Field ageField = clazz.getDeclaredField("age");
System.out.println(ageField);
//创建实例对象
Object obj = clazz.newInstance();
//访问私有对象
//对该私有字段进行取消权限检查的能力
ageField.setAccessible(true);//暴力访问
ageField.set(obj, 25);
Object o = ageField.get(obj);
System.out.println(o);
}
}