黑马程序员——反射
2015-08-13 12:16
591 查看
------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------
反射
(1)反射:在程序运行时期,是通过class文件对象,去使用构造方法,成员变量,成员方法。
(2)获取class文件对象
1:Object类的getClass()方法
2:数据类型的静态的class属性
3:Class类forName()静态方法
推荐:开发使用第三种。
整理反射应用的步骤并且写出代码
(3)反射的应用
1:通过反射获取构造方法并使用
//获取字节码文件对象
Class c = Class.forName("cn.itcast.Person");
//获取构造器对象
Constructor con = c.getDeclearedConstrator(String.class,int.class);
//通过构造器对象获取Person对象
Object obj = con.newInstance("张三",29);
2:通过反射获取成员变量并使用
//获取字节码文件对象
Class c = Class.forName("cn.itcast.Person");
//获取构造器对象
Constructor con = c.getDeclearedConstrator(String.class,int.class);
//通过构造器对象获取Person对象
Object obj = con.newInstance("张三",29);
//获取成员变量
Field field = c.getDeclaredField("name");
field.setAccessiable(true);
field.set(obj,"李四");
3:通过反射获取成员方法并使用
//获取字节码文件对象
Class c = Class.forName("cn.itcast.Person");
//获取构造器对象
Constructor con = c.getDeclearedConstrator(String.class,int.class);
//通过构造器对象获取Person对象
Object obj = con.newInstance("张三",29);
//获取所有公共方法对象,包括从父类继承的
//(获取本类所有方法的时候,不包括从父类继承的)
Method[] methods = c.getDeclaredMethods();
for(Method m : methods){
m.setAccessiable(true);
m.invoke(obj, null);
}
(4)案例
1、
public class Test {
public static void main(String[] args) throws Exception {
// Student s = new Student();
// s.love();
// Teacher t = new Teacher();
// t.love();
// 如果有需求上的改动,这里的代码一直在发生改动。
// 通过反射如何来实现呢
// 反射+配置文件
//作为配置文件来说
/*
* 键是固定的,是已经知道的。
* 值是变化的。
*
* className,methodName
*/
Properties prop = new Properties();
FileReader fr = new FileReader("test.properties");
prop.load(fr);
fr.close();
//获取类名
String className = prop.getProperty("className");
//获取方法名
String methodName = prop.getProperty("methodName");
//获取字节码文件对象
Class c = Class.forName(className);
Constructor con = c.getConstructor();
Object obj = con.newInstance();
Method m = c.getMethod(methodName, null);
m.invoke(obj, null);
}
}
2、在这个集合中添加一个字符串数据
import java.lang.reflect.Method;
import java.util.ArrayList;
/*
* 我给你ArrayList<Integer>的一个对象,我想在这个集合中添加一个字符串数据,如何实现呢?
* 通过反射实现。
*
* 反射可以越过泛型检查。
*/
public class ArrayListTest {
public static void main(String[] args) throws Exception {
ArrayList<Integer> array = new ArrayList<Integer>();
// array.add(10);
// array.add("hello");
// 获取字节码文件对象
Class c = array.getClass();
Method m = c.getMethod("add", Object.class);
m.invoke(array, "hello");
m.invoke(array, "world");
m.invoke(array, "java");
System.out.println(array);
}
}
3、
/*
* 反射:在运行状态下,通过class文件对象(Class的对象),去使用构造方法,成员变量,成员方法。
*
* 那么,我们是如何获取到class文件对象的呢?(字节码文件对象)
* A:Object类的getClass()方法。
* B:数据类型的静态的class属性
* C:通过Class类的静态方法forName(String className)
*
*
* 开发中用第三种。自己写例子测试可以使用前两种。
* 因为第三种方式可以结合配置文件使用。
*/
public class ReflectDemo {
public static void main(String[] args) throws ClassNotFoundException {
// 方式1
Person p = new Person();
Class c = p.getClass();
Person p2 = new Person();
Class c2 = p2.getClass();
System.out.print
98f1
ln(p == p2);// false
System.out.println(c == c2);// true
// 方式2
Class c3 = Person.class;
System.out.println(c == c3);// true
// 方式3
Class c4 = Class.forName("cn.itcast_01.Person");// true
System.out.println(c == c4);
}
}
------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------
反射
(1)反射:在程序运行时期,是通过class文件对象,去使用构造方法,成员变量,成员方法。
(2)获取class文件对象
1:Object类的getClass()方法
2:数据类型的静态的class属性
3:Class类forName()静态方法
推荐:开发使用第三种。
整理反射应用的步骤并且写出代码
(3)反射的应用
1:通过反射获取构造方法并使用
//获取字节码文件对象
Class c = Class.forName("cn.itcast.Person");
//获取构造器对象
Constructor con = c.getDeclearedConstrator(String.class,int.class);
//通过构造器对象获取Person对象
Object obj = con.newInstance("张三",29);
2:通过反射获取成员变量并使用
//获取字节码文件对象
Class c = Class.forName("cn.itcast.Person");
//获取构造器对象
Constructor con = c.getDeclearedConstrator(String.class,int.class);
//通过构造器对象获取Person对象
Object obj = con.newInstance("张三",29);
//获取成员变量
Field field = c.getDeclaredField("name");
field.setAccessiable(true);
field.set(obj,"李四");
3:通过反射获取成员方法并使用
//获取字节码文件对象
Class c = Class.forName("cn.itcast.Person");
//获取构造器对象
Constructor con = c.getDeclearedConstrator(String.class,int.class);
//通过构造器对象获取Person对象
Object obj = con.newInstance("张三",29);
//获取所有公共方法对象,包括从父类继承的
//(获取本类所有方法的时候,不包括从父类继承的)
Method[] methods = c.getDeclaredMethods();
for(Method m : methods){
m.setAccessiable(true);
m.invoke(obj, null);
}
(4)案例
1、
public class Test {
public static void main(String[] args) throws Exception {
// Student s = new Student();
// s.love();
// Teacher t = new Teacher();
// t.love();
// 如果有需求上的改动,这里的代码一直在发生改动。
// 通过反射如何来实现呢
// 反射+配置文件
//作为配置文件来说
/*
* 键是固定的,是已经知道的。
* 值是变化的。
*
* className,methodName
*/
Properties prop = new Properties();
FileReader fr = new FileReader("test.properties");
prop.load(fr);
fr.close();
//获取类名
String className = prop.getProperty("className");
//获取方法名
String methodName = prop.getProperty("methodName");
//获取字节码文件对象
Class c = Class.forName(className);
Constructor con = c.getConstructor();
Object obj = con.newInstance();
Method m = c.getMethod(methodName, null);
m.invoke(obj, null);
}
}
2、在这个集合中添加一个字符串数据
import java.lang.reflect.Method;
import java.util.ArrayList;
/*
* 我给你ArrayList<Integer>的一个对象,我想在这个集合中添加一个字符串数据,如何实现呢?
* 通过反射实现。
*
* 反射可以越过泛型检查。
*/
public class ArrayListTest {
public static void main(String[] args) throws Exception {
ArrayList<Integer> array = new ArrayList<Integer>();
// array.add(10);
// array.add("hello");
// 获取字节码文件对象
Class c = array.getClass();
Method m = c.getMethod("add", Object.class);
m.invoke(array, "hello");
m.invoke(array, "world");
m.invoke(array, "java");
System.out.println(array);
}
}
3、
/*
* 反射:在运行状态下,通过class文件对象(Class的对象),去使用构造方法,成员变量,成员方法。
*
* 那么,我们是如何获取到class文件对象的呢?(字节码文件对象)
* A:Object类的getClass()方法。
* B:数据类型的静态的class属性
* C:通过Class类的静态方法forName(String className)
*
*
* 开发中用第三种。自己写例子测试可以使用前两种。
* 因为第三种方式可以结合配置文件使用。
*/
public class ReflectDemo {
public static void main(String[] args) throws ClassNotFoundException {
// 方式1
Person p = new Person();
Class c = p.getClass();
Person p2 = new Person();
Class c2 = p2.getClass();
System.out.print
98f1
ln(p == p2);// false
System.out.println(c == c2);// true
// 方式2
Class c3 = Person.class;
System.out.println(c == c3);// true
// 方式3
Class c4 = Class.forName("cn.itcast_01.Person");// true
System.out.println(c == c4);
}
}
------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------
相关文章推荐
- 黑马程序员——OC笔记之购票系统应用实践
- 【黑马程序员】Foundation框架NSDictionary
- 程序员如何更快的适应新工作
- 面试题之变态跳台阶
- 黑马程序员——OC笔记之面向对象三大特征(封装、继承、多态)
- 黑马程序员——25,打印流,合并流,对象序列化,管道流,RandomAccessFile
- 十道海量数据处理面试题与十个方法大总结
- 《招聘一个靠谱的iOS》面试题参考答案(上)
- 【黑马程序员-学习笔记】OC-成员变量与属性
- 【我的技术我做主】8年IT职业生涯
- 栈内存和堆内存的一点小结 《程序员的自我修养》·笔记
- 腾讯面试题目之一
- 告诉你架构师与程序员的区别在哪里
- 面试题_53——正则表达式匹配
- 全新整理:微软、Google等公司的面试题及解答、第161-170题
- 九月腾讯,创新工场,淘宝等公司最新面试三十题(第171-200题)
- 全新整理:微软、Google等公司的面试题及解答、第161-170题
- 谈通过测试经验来识别开发中的问题
- 前进中不能迷失方向--Java程序员职业发展路线
- 微软面试100题2010年版全部答案集锦(含下载地址)