<span style="font-size:18px;"> 反射
一、 反射的定义
通俗的说,反射就是让你可以通过名称来得到对象 ( 类,属性,方法 ) 的技术。
二、 获取Class文件的方法
1. Class c1 = 类.Class, 实例:Class c1 = Worker.class;
2. Class c2 = 类对象.getClass(), 实例: Class c2 = worker.getClass();
3. Class c3 = Class.forName(路径) 实例: Class c3 = Class.forName(“reflect_07_06”);
注意:不管通过哪种方法获取Class对象,对于对于同一个类,Classd对象都是同一份。
三、 反射的几个重要的方法
<1>. 1. Constructor[] constructor = c3.getConstructors()
获取所有的公有的构造函数
2. Constructor constructor1 = c3.getConstructor(Class<?>…paraterTypes)
获取指定的构造函数
3. Constructor[] constructor2 = c3.getDeclaredConstructors()
获取所有修饰符修饰的构造函数
4. Constructor constructor3 = c3.getConstructor(Class<?>…paraterTypes)
获取指定的构造函数(包括私有的构造函数)</span>
<span style="font-size:18px;"> <2> 1. Method[] method = c.getMethods();
获取所有的公有方法(包括继承的)
2.Method method1 = c.getMethod("work");
获取指定公有方法
3.Method[] method = c.getDeclaredMethods();
获取本类中的所有修饰符修饰的普通方法(不包括继承的)
4. Method method1 = c.getDeclaredMethod("wokorMassage");
获取指定的方法
<3> 1. Field[] field = c.getFields();
获取公有的属性(本类的)
2. Field field1 = c.getField("age");
获取指定公有属性
3. Field[] field = c.getDeclaredFields();
获取所有修饰符修饰的属性
4. Field field1 = c.getDeclaredField("pay");
获取指定的属性
获取到构造方法后,可以实例化对象,若构造函数为私有的,则先要通过constructor3.setAccessible(true),设置修饰符为公有的,再实例化。
获取普通方法、属性后,与构造方法类似。
内省
一、 内省的定义
内省是Java语言对Bean类属性、事件的一种缺省处理方法。
二、 Bean类需要满足的条件
1. 类是具体的、公有的
2. 具有无参的构造函数
3. 属性的私有的
4. 对于定义的属性,提供get和set方法,且,get和set具有严格的定义要求:必须把属性的首字母大写在属性名前添加get和set方法。
三、 内省是通过反射机制来操作JavaBean的属性
1.通过类IntroSpector来获取某个对象的BeanInfo信息。
2.通过BeanInfo来获取属性的描述器(PropertyDescriptor)
3.通过属性描述器可以获取某个属性的get/set方法,通过反射机制来调用这些方法
四、 properties文件
<1>. properties文件定义
后缀名必须是. Properties的文件
格式 :key = value 实例: name1 = aa
<2>.几个常见的方法
1. getProperty(String key):通过参数key,获取对应的value值
2. load(inoutStream ins):通过对指定的文件,进行装载来获取对应文件中的所有数据
3. setProperty(String key ,String value):相当于Hashtable的方法put,用于设置键-值对
4. clear():清除所有装载的数据
五、反射实现工厂模式
1.在工厂模式中,加入内省机制,把属性放入Properties的文件中,代码中不再出现类名对象名与方法名。这样程序间的耦合度就大大减低,只与配置文件有较大的关联。以后只需修改Properties的文件
2.组成部分
<1>.抽象产品类
<2>.抽象产品具体类
<3>.工厂类
<4>. Properties的文件
3.实例
<1>.抽象产品类
public interface Arithmetic {
}
<2>.抽象产品具体类
2.1 加法:
public class AddBean implements Arithmetic{
private int x1;
private int x2;
public AddBean(){}
public int getX1() {
return x1;
}
public void setX1(int x1) {
this.x1 = x1;
}
public int getX2() {
return x2;
}
public void setX2(int x2) {
this.x2 = x2;
}
public void addIntOperation(){
System.out.println(x1+x2);
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "x1 = "+x1+" x2 = "+x2;
}
}
2.2 减法:
public class SubBean implements Arithmetic{
private int x1;
private int x2;
public SubBean(){}
public int getX1() {
return x1;
}
public void setX1(int x1) {
this.x1 = x1;
}
public int getX2() {
return x2;
}
public void setX2(int x2) {
this.x2 = x2;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "x1 = "+x1+" x2 = "+x2;
}
}
<3>.工厂类
public class ArithmeticFactory {
public static Properties properties = new Properties();
static{
try {
InputStream ins = ProppertiesTest.class.getResourceAsStream("/images/test.propreties");
System.out.println(ins);
properties.load(ins);
} catch (IOException e) {
e.printStackTrace();
}
}
public static Arithmetic createFactory(String type){
Arithmetic arithmetic = null;
try {
if("add".equals(type)){
arithmetic = (Arithmetic)Class.forName((String)properties.getProperty(type)).newInstance();
}else if("sub".equals(type)){
arithmetic = (Arithmetic)Class.forName((String)properties.getProperty(type)).newInstance();
}
BeanInfo beanInfo = Introspector.getBeanInfo(Class.forName((String)properties.getProperty(type)));
PropertyDescriptor[] propertyDescriptor = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor propertyDescriptor2 : propertyDescriptor) {
Method method = propertyDescriptor2.getWriteMethod();
if("class".equals(propertyDescriptor2.getName())){
continue;
}
method.invoke(arithmetic, Integer.parseInt((String) properties.get(propertyDescriptor2.getName())));
}
} catch (Exception e) {
e.printStackTrace();
}
return arithmetic;
}
}
<4>.测试类:
public class Test {
public static void main(String[] args) {
AddBean addBean = (AddBean) ArithmeticFactory.createFactory("add");
System.out.println(addBean+"%%%%%%%%%%%%%");
}
}
<5>. Properties的文件
add = date_07_10.AddBean
sub = date_07_10.SubBean;
x1 = 78
x2 = 45
结果:java.io.BufferedInputStream@576f8789
x1 = 78 x2 = 45%%%%%%%%%%%%%
</span>