通过反射机制,取得类的实例化对象
package com.cn.gaoyan;
class Person{
@Override
public String toString(){
return "Hello";
}
}
public class TestDemo {
public static void main(String args[]){
Class<?> demo1 = null;
Class<?> demo2 = null;
Class<?> demo3 = null;
//取得类的实例化对象,方法1
Person per = new Person();
demo1 = per.getClass();
//取得类的实例化对象,方法2
demo2 = Person.class;
//取得类的实例化对象,方法3
try {
demo3 = Class.forName("com.cn.gaoyan.Person");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("demo1"+ demo1.getName());
System.out.println("demo2"+ demo2.getName());
System.out.println("demo3" + demo3.getName());
//通过反射实例化对象
Object obj = null;
try {
obj = demo1.newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Person per1 = (Person)obj;
System.out.println(per1);
}
}
输出:
demo1com.cn.gaoyan.Person
demo2com.cn.gaoyan.Person
demo3com.cn.gaoyan.Person
Hello
将反射运用于工厂模式
package com.cn.gao;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
interface Fruit{
public void eat();
}
class Apple implements Fruit{
public Apple(){}
@Override
public void eat() {
// TODO Auto-generated method stub
System.out.println("eat apple");
}
}
class Orange implements Fruit{
@Override
public void eat() {
// TODO Auto-generated method stub
System.out.println("eat orange");
}
}
class Factory {
public static Fruit getInstance(String className){
Fruit f = null;
try {
f = (Fruit)Class.forName(className).newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return f;
}
}
public class DemoFactory {
public static void main(String args[]){
Fruit f = (Fruit)Factory.getInstance("com.cn.gao.Apple");
//取得该对象的构造函数
System.out.println("**********取得该对象的构造函数");
Constructor<?>[] cls = f.getClass().getConstructors();
for(int i = 0; i < cls.length; i++){
System.out.println(cls[i]);
}
//取得该对象所在类实现的接口
System.out.println("**********取得该对象所在类实现的接口");
Class<?> cls1[] = f.getClass().getInterfaces();
for(int i = 0; i < cls1.length; i++){
System.out.println(cls1[i]);
}
//取得该对象的普通方法
System.out.println("**********取得该对象的普通方法");
Method getMethod = null;
try {
getMethod = f.getClass().getMethod("eat");
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(getMethod);
//通过反射机制调用该类的普通方法
System.out.println("**********通过反射机制调用该类的普通方法");
try {
getMethod.invoke(f, null);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("**********调用该类的普通方法");
f.eat();
}
}
输出:
**********取得该对象的构造函数
public com.cn.gao.Apple()
**********取得该对象所在类实现的接口
interface com.cn.gao.Fruit
**********取得该对象的普通方法
public void com.cn.gao.Apple.eat()
**********通过反射机制调用该类的普通方法
eat apple
**********调用该类的普通方法
eat apple