黑马程序员——反射

------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------
反射,对class字节码文件对象进行解剖,获取成员并运行,后期的扩展性很强,经过配置文件来实现程序功能。


代码:
/*
 * 反射通过配置文件运行
 */
import java.io.*;
import java.util.*;
import java.lang.reflect.*;
public class ReflectTest {
public static void main(String[] args)throws Exception {
//创建集合对象
Properties pro = new Properties();
//创建IO流,读取配置文件
FileReader fr = new FileReader("c:\\config.properties");
//集合IO管理
pro.load(fr);

for(int x = 1 ; x <= pro.size() / 2; x++){
//通过键值对,获取值
String className = pro.getProperty("className"+x);
String methodName = pro.getProperty("methodName"+x);
//反射获取字节码文件对象
Class clazz = Class.forName(className);
//创建对象
Object obj = clazz.newInstance();
//获取方法
Method method = clazz.getMethod(methodName);
method.invoke(obj);
}
fr.close();
}
}




class类的方法:


Constrctr[] getConstructors获取构造,是公共的。


Construetor getConstructos(参数列表)指定的构造方法。


获取私有的构造方法,get后面加上Declared

newlnstance()空参数的构造,public权限


Method[] getMethod()公共的,继承和实现的方法。


Method getMethod(方法名,参数列表)指定的方法。

获取私有的方法,get后面加上Declared


代码列举其中一方法:




import java.util.*;
import java.lang.reflect.*;
public class ListDemo {
public static void main(String[] args) throws Exception{


ArrayList<String> array = new ArrayList<String>();
array.add("asdf");
array.add("dgfer");
       //获取ArrayList类的字节码文件对象
Class clazz = array.getClass();
//获取添加方法add
Method method = clazz.getMethod("add", Object.class);
method.invoke(array, 123);
method.invoke(array, 12.34);
method.invoke(array, true);
System.out.println(array);
Iterator it = array.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
for(Object s : array){
System.out.println(s);
}

}
}
获取字节码文件对象的三个方法,返回Class类型:

对象的获取;


类名获取.class

Class.forName() 获取,一般常用这个,也推荐使用这个方法


Class.forName()举例代码:


public class ReflectDemo {
public static void main(String[] args) throws Exception{
//第一种方式,通过对象获取
Person p = new Person();
//Object类中有一个方法 getClass()返回值是 Class类型
Class c1 = p.getClass();
System.out.println(c1);

//第二种方式,通过类的静态属性class获取,返回这个类的Class类型,字节码文件对象
Class c2 = Person.class;
System.out.println(c2);

//第三种方式,通过Class类的静态方法forName获取--最重要
Class c3 = Class.forName("cn.itcast.reflect.Person");
System.out.println(c3);
}
}
Constructor类:


newInsurance(构造方法的实际参数)创建对象
代码:
package cn.itcast.reflect;
/*
 * 获取构造方法并运行
 */
import java.lang.reflect.*;
public class ReflectDemo1 {
public static void main(String[] args) throws Exception{

method_2();
}
//获取构造方法的简单运行,保证类中有public空参数构造
private static void method_2()throws Exception{
Class c = Class.forName("cn.itcast.reflect.Person");
Object obj = c.newInstance();
System.out.println(obj);
}

//获取私有构造方法,并运行
private static void method_1()throws Exception{
Class c = Class.forName("cn.itcast.reflect.Person");
// Constructor<?>[] getDeclaredConstructors() 
/*Constructor[] cons = c.getDeclaredConstructors();
for(Constructor con : cons){
System.out.println(con);
}*/
//获取私有的,带有String,int参数构造
Constructor con = c.getDeclaredConstructor(String.class,int.class);
System.out.println(con);
con.setAccessible(true);
Object obj = con.newInstance("zhangsan",18);
System.out.println(obj);
}

//获取的构造方法,是public
private static void method()throws Exception{
Class c = Class.forName("cn.itcast.reflect.Person");
//Constructor[] getConstructors
/*Constructor[] cons = c.getConstructors();
for(Constructor con : cons){
System.out.println(con);
}*/
//获取空参数的构造方法,并运行
Constructor con = c.getConstructor();
System.out.println(con);
Object obj = con.newInstance();
System.out.println(obj.toString());

//获取带有int参数的构造方法,并运行
Constructor conint = c.getConstructor(int.class);
System.out.println("====="+conint);
Object obj2 = conint.newInstance(19);
System.out.println(obj2.toString());
}
}
Method类:
invoke(对象,方法的实际参数)运行获取到的方法


通过配置文件运行。




没有学不好,只有不认真~~~----至自己!



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值