本文目录:一、自己编写的反射练习的Demo;
二、编写一个实例,如何通过反射获取一个类中private 字段的值
-------------------------------------------------------------------------------------------------------------------------------------------
一、自己编写的反射练习的Demo;
建立一个实体:Book.java
package testdb;
import java.math.BigDecimal;
public class Book {
private int id;
private String book_Type;
private String book_Name;
public BigDecimal sale_Price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBook_Type() {
return book_Type;
}
public void setBook_Type(String book_Type) {
this.book_Type = book_Type;
}
public String getBook_Name() {
//System.out.println("掉眼泪该方法,book_Name:"+book_Name);
return book_Name;
}
public void setBook_Name(String book_Name) {
this.book_Name = book_Name;
}
public BigDecimal getSale_Price() {
return sale_Price;
}
public void setSale_Price(BigDecimal sale_Price) {
this.sale_Price = sale_Price;
}
}
2、编写实例 main 类
package testdb;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public class BookTest {
//------------Class 3种方式获得类对象
public void classTest() throws InstantiationException, IllegalAccessException{
Class demo1=null;
Class demo2=null;
Class demo3=null;
try {
//1、类对象 Class.forName()
demo1 = Class.forName("testdb.Book");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("第一种方式(Class.forName()):-----------"+demo1);
//-------------------方式2开始------------------------------
Book book = new Book();
Object ob =book;
System.out.println("第2种方式:ob.getClass()= ---------------"+ob.getClass());
System.out.println("第2种方式:book.getClass()= ---------------"+book.getClass());
//---------------------方式3开始-------------------------------------
demo3 = Book.class;
System.out.println("第三种方式获取类对象(demo3 = Book.class;):----------"+demo3);
//-----------------------将类对象实例化-----------------------------------------------
System.out.println("将类对象实例化: "+demo3.newInstance());
}
//------------Field
public void showClass( Class cl ){
Field [] fl = cl.getDeclaredFields();//得到类的所有对象(可以将私有属性获取出来)
for(Field fl1:fl){
System.out.println("对象名称----"+fl1.getName()); fl1.getName();//获取对象的名称
System.out.println("对象类型----"+fl1.getType());fl1.getType();//获取属性对象类型
}
System.out.println("-----------------------------------------------------------");
Field [] fle = cl.getFields();//得到类的所有对象(只可以将公有属性获取出来)
for(Field fl1:fle){
System.out.println("对象名称222----"+fl1.getName()); fl1.getName();//获取对象的名称
System.out.println("对象类型----"+fl1.getType());fl1.getType();//获取属性对象类型
}
}
public void showClass(Object ob) throws IllegalArgumentException, IllegalAccessException{
Class cl = ob.getClass();
Field [] fl = cl.getDeclaredFields();//得到类的所有对象(可以将私有属性获取出来)
for(Field fl1:fl){
fl1.setAccessible(true);
System.out.println("对象名称ob----"+fl1.getName()+"---"+fl1.get(ob)); fl1.getName();//获取对象的名称
}
}
//------------------------Method1------------获取类对象方法
public void showMethod(Object ob) throws ClassNotFoundException{
Class cl = ob.getClass();
Method [] methods = cl.getDeclaredMethods();//获得对象的所有方法
for(Method med:methods){
System.out.println("方法名----"+med.getName()); //获取对象方法名
System.out.println("方法修饰符----"+Modifier.toString(med.getModifiers()));//public private ...
System.out.println("方法返回值类型----"+med.getReturnType());//方法返回值
//方法参数列表 set方法
Class[] proType= med.getParameterTypes();
System.out.println("方法参数列表");
for(Class type:proType){
System.out.println("参数类型:"+type.getName());
}
}
}
//--------------------Method2---------------------------操作类对象值
public void showMethod2(Object ob) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
Class cl = ob.getClass();
Method me = cl.getMethod("getBook_Name", null);
me.invoke(ob, null);
System.out.println(me.invoke(ob, null));
System.out.println("---------------------------------");
Method me2 = cl.getMethod("setBook_Name", String.class);
me2.invoke(ob, "平凡的世界");//相当于book.setBook_Name 设值方法
Method me3 = cl.getMethod("getBook_Name", null);
me3.invoke(ob, null);
System.out.println(me3.invoke(ob, null));//相当于book.getBook_Name 取值方法
}
/**
* @param args
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws InstantiationException
* @throws InvocationTargetException
* @throws NoSuchMethodException
* @throws SecurityException
*/
public static void main(String[] args) throws ClassNotFoundException, IllegalArgumentException, IllegalAccessException, InstantiationException, SecurityException, NoSuchMethodException, InvocationTargetException {
// TODO Auto-generated method stub
//新建一本书
Book book = new Book();
Class cl= Class.forName("java.lang.String");
book.setId(1);
book.setBook_Name("《和空姐同居的日子》");
book.setBook_Type("言情");
//
BookTest bt = new BookTest();
//bt.classTest();
//bt.showClass(book);
//bt.showClass(Class.forName("testdb.Book"));
bt.showMethod2(book);
System.out.println(book.getBook_Name());
}
}
二、编写一个实例,如何通过反射获取一个类中private 字段的值
1、编写一个实体:
package ww;
public class BeanTest {
private int count;
public <span style="font-family: Arial, Helvetica, sans-serif;">BeanTest</span><span style="font-family: Arial, Helvetica, sans-serif;">(String str) {</span>
super();
this.count = str.length();
}
}
2、main 代码:
public class demoTest {
public static void main(String[] args) throws Exception{
<span style="font-family: Arial, Helvetica, sans-serif;">beanTest</span><span style="font-family: Arial, Helvetica, sans-serif;"> </span><span style="font-family: Arial, Helvetica, sans-serif;"> test = new </span><span style="font-family: Arial, Helvetica, sans-serif;">beanTest</span><span style="font-family: Arial, Helvetica, sans-serif;">("abcdsef");</span>
Field f = beanTest.class.getDeclaredField("count");
//其中,最关键的代码是:
//f.setAccessible(true);
//这行代码把对象data上的name字段设置为public访问属性.
f.setAccessible(true);
System.out.println(f.get(test));
}
}
运行结果:7
PS----确实不是很清楚,java 的一大特性就是封装,保障数据安全; 但是这里却能通过反射访问被封装的数据。