java反射 练习题
package hello;
import java.io.FileReader;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Scanner;
public class Reflect {
public static void main(String[] args) throws Exception {
// fanxingDemo();
// newObject1();
// newObject2();
// printStr();
// showObj();
// setProperty(new Book(),"title","张三");
// beanDemo();
// run();
System.out.println(getProperty(new Book("xiao受那个书", 40),"title"));
}
// 8. 写一个方法,此方法可以获取obj对象中名为propertyName的属性的值
private static Object getProperty(Book book, String title) throws Exception {
Class c = book.getClass();
Field field = c.getDeclaredField(title);
field.setAccessible(true);
Object f = field.get(book);
return f;
}
// 7.(1)写一个Properties格式的配置文件,配置类的完整名称。
// (2) 写一个程序,读取这个Properties配置文件,获得类的完整名称并加载这个类,
// (3)用反射 的方式运行run方法。
private static void run() throws Exception {
Properties pro = new Properties();
// 注意:如果是用Eclise的话,切记要右键工程名字,新建一个file文件,因为如果file文件不在工程根目录的话,它是找不到的。
FileReader fr = new FileReader("config.properties");
pro.load(fr);
fr.close();
String className = pro.getProperty("className");
String MethodName = pro.getProperty("MethodName");
Class c = Class.forName(className);
Object obj = c.newInstance();
Method method = c.getMethod(MethodName);
method.invoke(obj);
}
// 6.定义一个标准的JavaBean,名叫Person,包含属性name、age。
// 使用反射的方式创建一个实例、调用构造函数初始化name、age,使用反射方式调用setName方法对名称进行设置,
// 不使用setAge方法直接使用反射方式对age赋值。
private static void beanDemo() throws Exception {
Class c = Class.forName("hello.Book");
Constructor cons = c.getConstructor(String.class,int.class);
Object obj = cons.newInstance("xiaohognshu",30);
Method method = c.getDeclaredMethod("setTitle", String.class);
method.invoke(obj, "小红书"); // 反射方式调用setName方法对名称进行设置
System.out.println(obj);
Field field = c.getDeclaredField("num");
field.setAccessible(true);
field.set(obj, 20);
System.out.println(obj);
}
// 5. 写一个方法,此方法可将obj对象中名为propertyName的属性的值设置为value.
private static void setProperty(Object obj, String propertyName, String value) throws Exception {
Class cla = obj.getClass();
Field field = cla.getDeclaredField(propertyName); // 获得包括私有和公有的属性
field.setAccessible(true); // 还要配合这个,取消权限检查,才能设置属性
field.set(obj,propertyName);
System.out.println(obj);
}
// 4.编写一个类A,增加一个实例方法showString,用于打印一条字符串,在编写一个类TestA
// ,作为客户端,用键盘输入一个字符串,该字符串就是类A的全名,
// 使用反射机制创建该类的对象,并调用该对象中的方法showString
private static void showObj() throws Exception {
Scanner scaner = new Scanner(System.in);
System.out.println("输入一个全类名:");
String str = scaner.nextLine();
Class cla = Class.forName(str);
Object obj = cla.newInstance();
Method method = cla.getMethod("getTitle");
method.invoke(obj);
System.out.println(obj);
}
// 3. 编写一个类,增加一个实例方法用于打印一条字符串。并使用反射手段创建该类的对象, 并调用该对象中的方法。
private static void printStr() throws Exception {
Class cla = Class.forName("hello.Book");
Object book = cla.newInstance();
Method method = cla.getMethod("toString");
method.invoke(book);
System.out.println(book);
}
// 2.用反射创建对象,用两种方法实现
private static void newObject1() throws Exception {
Class cla = Class.forName("hello.Book");
Constructor cons = cla.getConstructor(String.class,int.class);
Object obj = cons.newInstance("xihaongshu",30);
System.out.println(obj);
}
private static void newObject2() throws Exception {
Class cla = Class.forName("hello.Book");
Object obj = cla.newInstance();
System.out.println(obj);
}
// 1.泛型类型擦除
private static void fanxingDemo() throws Exception {
ArrayList<String> arrays = new ArrayList<String>();
arrays.add("begin");
Class cla = Class.forName("java.util.ArrayList");
Method method = cla.getMethod("add", Object.class);
method.invoke(arrays, "ttt");
for(Object obj:arrays) {
System.out.println(obj);
}
}
}