死亡笔记4--反射笔记 知识点+面试题

什么是反射?


一个类有多个组成部分,例如:构造方法  成员变量  方法等,反射就是加载类,并解剖出类的各个组成部分.


加载类,获得类的字节码的方式:
1 Class clazz1=Class.forName("com.test.reflect.Person");
2 Class clazz2=Person.class
3 Class clazz3=new Person().getClass()


私有的东西,在类外面能被访问吗?
不可以,但是在反射中可以做到这一点.
(c.setAccressible(true)  暴力反射)


反射出类的无参构造来创建对象:
Class clazz=Class.forName("com.test.reflect.Person");
Person p=clazz.newInstance();
(定义一个类的时候,最好定义无参构造)


静态的方法,在发射的时候,不需要创对象,直接传null就可以


反射的使用:
1 通过反射获取构造方法并使用
2 通过反射获取成员变量并使用
3 通过反射获取成员方法并使用


反射的案例:
通过反射运行配置文件的内容
通过反射越过泛型检查
通过反射给任意一个对象的任意的属性复制为指定的值






反射指定类中的方法:
//获取类中所有的方法。
public static void method_1() throws Exception {
Class clazz = Class.forName("cn.itcast.bean.Person");
Method[] methods = clazz.getMethods();//获取的是该类中的公有方法和父类中的公有方法。
methods = clazz.getDeclaredMethods();//获取本类中的方法,包含私有方法。
for(Method method : methods) {
System.out.println(method);
}
}
//获取指定方法;
public static void method_2() throws Exception {
Class clazz = Class.forName("cn.itcast.bean.Person");
//获取指定名称的方法。
Method method = clazz.getMethod("show", int.class,String.class);
//想要运行指定方法,当然是方法对象最清楚,为了让方法运行,调用方法对象的invoke方法即可,但是方法运行必须要明确所属的对象和具体的实际参数。
Object obj = clazz.newInstance();
method.invoke(obj, 39,"hehehe");//执行一个方法
}
//想要运行私有方法。
public static void method_3() throws Exception {
Class clazz = Class.forName("cn.itcast.bean.Person");
//想要获取私有方法。必须用getDeclearMethod();
Method method = clazz.getDeclaredMethod("method", null);
// 私有方法不能直接访问,因为权限不够。非要访问,可以通过暴力的方式。
method.setAccessible(true);//一般很少用,因为私有就是隐藏起来,所以尽量不要访问。
}
//反射静态方法。
public static void method_4() throws Exception {
Class clazz = Class.forName("cn.itcast.bean.Person");
Method method = clazz.getMethod("function",null);
method.invoke(null,null);
}



反射案例:
面试题1:
/*
 * 编写一个类,增加一个实例方法用于打印一条字符串。
 * 并使用反射手段创建该类的对象, 并调用该对象中的方法。
 */
public class Demo1 {
public static void main(String[] args) throws Exception{
Method method=Class.forName("heimatest.MyTest").getMethod("show", String.class);
method.invoke(Class.forName("heimatest.MyTest").newInstance(), "hahahahaha");
}
}
class MyTest{
public void show(String str){
System.out.println(str);
}
}


面试题2:


/*
 *  现有如下两个已知条件:
 * 在F盘有一个文件:a.txt ,该文件内容如下:
 * test.Person,id:5
 * test.Student,id:6
 * 
 * (1)读取a.txt中为每一行,把每一行放入一个变量中
 * (2)用","分隔符把变量变成一个String类型的数组。
 * (3)读取数据中的第一个元素,利用java反射机制创建对象。
 */
public class Demo2 {
public static void main(String[] args) throws Exception {
BufferedReader br=new BufferedReader(new FileReader("g:\\a.txt"));
String line1=br.readLine();
String line2=br.readLine();
String[] str1=line1.split(",");
String[] str2=line2.split(",");
Class clazz=Class.forName(str1[0]);
Object obj=clazz.newInstance();

}
}


面试题3:
在集合中越过泛型检查,并添加对象
/* ArrayList<Integer> list = new ArrayList<Integer>(); 
在这个泛型为Integer的ArrayList中存放一个String类型的对象。
*/
public class Demo3 {
public static void main(String[] args) throws Exception {
ArrayList<Integer> list=new ArrayList<Integer>();
Class clazz=list.getClass();
Method method=clazz.getMethod("add", Object.class);
method.invoke(list, "hello,I'm a String!!");
System.out.println(list.get(0));
}
}




面试题4:


//写一个类A,增加一个实力方法showString,用于打印一条字符串,在编写一个类TestA ,作为客户端,用键盘输入一个字符串,改字符 
//串就是类A的全名,使用反射机制创建该类的对象,并调用该对象中的方法showString。
public class Demo4 {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
String string = sc.nextLine();
Class c = Class.forName("heima.mianshiti.A");// 获取字节码对象
Method m = c.getMethod("showString", String.class);
m.invoke(c.newInstance(), string);
}
}


class A {
public void showString(String str) {
System.out.println(str);
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值