@snail-lb
2017-02-28T23:22:47.000000Z
字数 6409
阅读 934
java进阶
本文采用例子进行学习,直接看示例程序应该就能看懂。
用于操作的Student类
package com.cn.reflect;
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name,int age) {
this.name = name;
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void display() {
System.out.println("学生信息: name=" + name + " age=" + age);
}
public void say(String str){
System.out.println("学生说:" + str);
}
}
使用的反射对学生类进行操作
package com.cn.reflect;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
/**
* java反射
*
* @author lvbiao
*
*/
public class TestReflect {
public static void main(String[] args) {
Class test = null;
try {
test = Class.forName("com.cn.reflect.Student");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//获取完整类名
System.out.println("------获取完整类名----------");
System.out.println("完整类名:" + getMothodName(test));
//无参构造实例化对象
System.out.println("------无参构造实例化对象----------");
Student stu1 = (Student) getObjectNoParameters(test);
stu1.display();
//通过其他构造函数实例化对象
System.out.println("------通过其他构造函数实例化对象-----");
Student stu2 = (Student) getObjectHaveParameters(test);
stu2.display();
//获取类全部属性信息
System.out.println("------获取类全部属性信息----------");
AttributeInfo(test);
//获取全部方法信息
System.out.println("------获取全部方法信息-----------");
MethodInfo(test);
//调用类中无参方法
System.out.println("------调用类中无参方法-----------");
callMethodNoParameters(test,"display");
//调用类中有参方法
System.out.println("------调用类中有参方法-----------");
callMethodHaveParameters(test,"say","hello");
//通过反射来操作属性
System.out.println("------通过反射来操作属性,设置名字为Bob-----------");
setAttribute(test,"name","Bob");
}
// 获取完整类名
public static String getMothodName(Class cla) {
return cla.getName();
}
// 通过无参构造 实例化对象
public static Object getObjectNoParameters(Class cla) {
try {
return cla.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
// 通过有参构造 实例化对象
public static Object getObjectHaveParameters(Class cla) {
Constructor<?> cons[] = cla.getConstructors();
Object obj = null;
try {
obj = cons[1].newInstance("张三",23);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return obj;
}
//获取类全部属性信息
public static void AttributeInfo(Class cla){
Field[] field = cla.getDeclaredFields();
for (int i = 0; i < field.length; i++) {
// 权限修饰符
int mo = field[i].getModifiers();
String priv = Modifier.toString(mo);
// 属性类型
Class<?> type = field[i].getType();
System.out.println(priv + " " + type.getName() + " "
+ field[i].getName() + ";");
}
}
//获取类中方法
public static void MethodInfo(Class cla){
Method[] method = cla.getDeclaredMethods();
for (int i = 0; i < method.length; i++) {
// 权限修饰符
int mo = method[i].getModifiers();
String priv = Modifier.toString(mo);
//获取返回类型
String retu = method[i].getReturnType().getName();
// 属性类型
String name = method[i].getName();
System.out.println(priv + " " + retu + " "
+ name + "()");
}
}
//调用无参方法
public static void callMethodNoParameters(Class cla,String methodName){
try {
Method method1 = cla.getMethod(methodName);
method1.invoke(cla.newInstance());
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
//调用有参方法
public static void callMethodHaveParameters(Class cla,String methodName,String parameter){
try {
Method method2=cla.getMethod(methodName, String.class);
method2.invoke(cla.newInstance(),parameter);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
//通过反射来操作属性
public static void setAttribute(Class cla,String key,String value){
try {
Object obj = cla.newInstance();
Field field = cla.getDeclaredField(key);
field.setAccessible(true);
field.set(obj,value);
//调用方法来验证是否设置成功
Method method = cla.getMethod("display");
method.invoke(obj);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
动态代理的项目接口
package com.cn.reflect.DynamicProxy;
public interface Subject {
public void say(String name,int age);
}
项目实现
package com.cn.reflect.DynamicProxy;
public class RealSubject implements Subject{
@Override
public void say(String name, int age) {
System.out.println(name + "今年已经" + age + "岁了。");
}
}
代理实现类
package com.cn.reflect.DynamicProxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class MyInvocationHandler implements InvocationHandler {
private Object obj = null;
public Object bind(Object obj) {
this.obj = obj;
return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object temp = method.invoke(this.obj, args);
return temp;
}
}
测试类
package com.cn.reflect.DynamicProxy;
public class Test {
public static void main(String[] args) {
MyInvocationHandler demo = new MyInvocationHandler();
Subject sub = (Subject) demo.bind(new RealSubject());
sub.say("Rollen",23);
}
}
1.公共接口
package com.cn.reflect.FactoryPattern;
public interface Fruit {
public abstract void eat();
}
2.实现类一
package com.cn.reflect.FactoryPattern;
public class Apple implements Fruit {
@Override
public void eat() {
System.out.println("吃苹果");
}
}
3.实现类二
package com.cn.reflect.FactoryPattern;
public class Orange implements Fruit{
@Override
public void eat() {
System.out.println("吃橘子");
}
}
4.工厂方法
package com.cn.reflect.FactoryPattern;
public class FruitFactory {
public static Fruit getInstance(String ClassName){
Fruit fruit = null;
try {
fruit = (Fruit) Class.forName(ClassName).newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return fruit;
}
}
5.测试类
package com.cn.reflect.FactoryPattern;
public class Test {
public static void main(String[] args) {
Fruit apple = FruitFactory.getInstance("com.cn.reflect.FactoryPattern.Apple");
apple.eat();
}
}