Java反射机制(未完成,还缺最后一个)

1、背景

            1)Reflection也就是反射  是Java被视为动态(或准动态)语言的一个关键性质

            2)反射机制指的是程序在运行时能够获取任何类的内部所有信息

2、实现功能概述

            1)只要给定类的全名,即可通过反射获取类的所有信息。

            2)反射可以在程序运行时获取任意一个对象所属的类对象。

            3)在运行时可以获取到类中所有属性对象,并对其操作(包括私有属性)。

            4)在运行时可以获取到类中,父类中所有方法,并调用。

            5)目前主流的应用框架如Struts2,Hibernate,Spring,SpringMVC等框架的核心全部是利用Java的反射机制来实现的。

Class对象的机制与实现

    1、Class对象概述

            1)Class其实就是类的类型

            2)字符串类型就是String,整形类型就是Integer,String和Integer类型就是Class

2、Class对象的常用方法介绍

            211717_wmXw_2356966.png

                newInstance()             实例化对象

3、实例化对象的三种方式

JavaBean

public class Book {
	private int id;
	private String name;
	public String type;
	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the type
	 */
	public String getType() {
		return type;
	}
	/**
	 * @param type the type to set
	 */
	public void setType(String type) {
		this.type = type;
	}
	
}

package main;

import bean.Book;

public class Test1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Class demo1=null;
		Class demo2=null;
		Class demo3=null;
		//实例化对象的第一种方式
		try{
			demo1=Class.forName("bean.Book");
		}catch(Exception e){
			e.printStackTrace();
		}
		System.out.println(demo1);
		//实例化对象的第二种方式
		Book bo=new Book();
		Object ob=bo;
		System.out.println("第二种"+ob.getClass());
		//实例化对象的第三种方式
		demo3=Book.class;
		System.out.println("第三种"+demo3);
		//
		
		
		try {
			Book bo1=(Book)demo3.newInstance();
			System.out.println("实例化后的类对象:"+bo1);
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

214513_WRLT_2356966.png

Field 对象的机制与实现

215542_xV3w_2356966.png

package main;

import java.lang.reflect.Field;

import bean.Book;

public class FieldTest {
	//该方法用于使用传递过来的Class对象获取类中的属性
	public void show(Class c1){
		
		Field[] fi=c1.getDeclaredFields(); //{可以将私有属性获取到} getDeclaredFields()获取类中的所有属性
		for(Field ff:fi){
			System.out.println(ff.getName());
			System.out.println(ff.getType());
		}
		System.out.println("~~~~~~~~~~~~~~~~~");
		
		Field[] fi1=c1.getFields();			//只可以获取到公有属性
		for(Field ff1:fi1){
			System.out.println(ff1.getName());
			System.out.println(ff1.getType());
		}
		System.out.println("~~~~~~~~~~~~~~~~~");
	}
	//该方法用于使用传递过来的实体类对象 获取属性以及属性的值
	public void show(Object ob){
		Class c1=ob.getClass();
		Field[] fi=c1.getDeclaredFields();
		for(Field ff:fi){
			try {
				ff.setAccessible(true);
				System.out.println(ff.getName()+":"+ff.get(ob));
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Book bo=new Book();
		bo.setId(1);
		bo.setName("倾城之恋");
		bo.setType("文学");
		
		FieldTest ft=new FieldTest();
		ft.show(Book.class);
		
		Book oob=new Book();
		oob.setId(2);
		oob.setName("那年夏天");
		oob.setType("文学");
		
		FieldTest ft1=new FieldTest();
		ft1.show(oob);
	}

}

230012_Fj1M_2356966.png

230343_52XQ_2356966.png

getDeclaredAnnotations()   获取方法的全部注解

package main;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import bean.Book;

public class MethodTest {
	//该方法用于获取对象的所有方法名称、返回值类型、以及参数信息
	public void show(Object ob){
		Class c2=ob.getClass();
		Method[] mt=c2.getDeclaredMethods();
		for(Method mm:mt){
			System.out.println("方法名称:"+mm.getName());
			System.out.println("方法修饰符"+Modifier.toString(mm.getModifiers()));
			System.out.println("方法返回值类型"+mm.getReturnType());
			System.out.println("方法参数列表");
			Class[] preType=mm.getParameterTypes();
			for(Class c21:preType){
				System.out.println(c21.getName());
			}
		
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Book bk=new Book();
		bk.setId(3);
		bk.setName("我和凤姐居住的日子");
		bk.setType("惊悚");
		MethodTest mt=new MethodTest();
		mt.show(bk);
	}

}

233122_9GC3_2356966.png

Method 对象的机制与实现

package main;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import bean.Book;

public class MethodTest {
	//该方法用于获取对象的所有方法名称、返回值类型、以及参数信息
	public void show(Object ob){
		Class c2=ob.getClass();
		Method[] mt=c2.getDeclaredMethods();
		for(Method mm:mt){
			System.out.println("方法名称:"+mm.getName());
			System.out.println("方法修饰符"+Modifier.toString(mm.getModifiers()));
			System.out.println("方法返回值类型"+mm.getReturnType());
			System.out.println("方法参数列表");
			Class[] preType=mm.getParameterTypes();
			for(Class c21:preType){
				System.out.println(c21.getName());
			}
		
		}
	}
	//该方法用于使用传递过来的实体对象 获取其中的方法 并调用 
	public void showUse(Object ob){
		Class c1=ob.getClass();
		try {
			Method me=c1.getMethod("getName", null);
			try {
				me.invoke(ob, new Object[0]);
			} catch (IllegalAccessException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			Method me1=c1.getMethod("setName", String.class);
			try {
				me1.invoke(ob, "嘻游记");
				Class[] c4={String.class,int.class};
				Method me2=c1.getMethod("test", c4);
				Object[] obb={"哈哈",12};
				me2.invoke(ob, obb);
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}  catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Book bk=new Book();
		bk.setId(3);
		bk.setName("我和凤姐居住的日子");
		bk.setType("惊悚");
		
		//MethodTest mt=new MethodTest();
		//mt.show(bk);
		
		MethodTest mte=new MethodTest();
		mte.showUse(bk);
		System.out.println(bk.getName());
	}

}

000635_jsFa_2356966.png


Excel 导入、导出的实现  

package main2;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import bean2.Book;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class ExcelDemo01 {
	public void excelOut(){
		WritableWorkbook book=null;//Excle对象
		try {
			//创建excle对象
			book=Workbook.createWorkbook(new File("D:/eclipse/workspace/excel/book.xls"));
			//通过excle对象创建一个选项卡对象
			WritableSheet sheet=book.createSheet("sheet1", 0);
			//创建一个单元格对象  列  行  值
			Label la=new Label(0,2,"test");
			//将创建好的单元格对象放入选项卡中
			try {
				sheet.addCell(la);
			} catch (RowsExceededException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (WriteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//写入目标路径
			book.write();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				book.close();
			} catch (WriteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ExcelDemo01 eb=new ExcelDemo01();
	
		eb.excelOut();
	}

}

192027_tcHH_2356966.png

192027_r7iz_2356966.png

package main2;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import bean2.Book;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class ExcelDemo01 {
	public void excelOut(ArrayList ar){
		WritableWorkbook book=null;//Excle对象
		try {
			//创建excle对象
			book=Workbook.createWorkbook(new File("D:/eclipse/workspace/excel/book.xls"));
			//通过excle对象创建一个选项卡对象
			WritableSheet sheet=book.createSheet("sheet1", 0);
			//创建一个单元格对象  列  行  值
			for(int i=0;i<ar.size();i++){
				Book bo=(Book) ar.get(i);
				Label la1=new Label(0,i,String.valueOf(bo.getId()));
				Label la2=new Label(1,i,bo.getName());
				Label la3=new Label(2,i,bo.getType());
				sheet.addCell(la1);
				sheet.addCell(la2);
				sheet.addCell(la3);
			}
			book.write();
		}catch (RowsExceededException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (WriteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				book.close();
			} catch (WriteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ExcelDemo01 eb=new ExcelDemo01();
		ArrayList<Book> ar=new ArrayList<Book>();
		Book bo=new Book();
		bo.setId(1);
		bo.setName("月子");
		bo.setType("生活");
		Book bo2=new Book();
		bo2.setId(2);
		bo2.setName("月子");
		bo2.setType("生活");
		Book bo3=new Book();
		bo3.setId(3);
		bo3.setName("月子");
		bo3.setType("生活");
		ar.add(bo);
		ar.add(bo2);
		ar.add(bo3);

		eb.excelOut(ar);
	}

}

193920_bJiP_2356966.png

package main2;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import bean2.Book;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class ExcelDemo01 {
	
	public ArrayList<Book> excleIn(){
		ArrayList<Book> ar=new ArrayList<Book>();
		Workbook book=null;
		try {
			//获取到excle对象
			book=Workbook.getWorkbook(new File("D:/eclipse/workspace/excel/book.xls"));
			Sheet sheet=book.getSheet(0);
			for(int i=0;i<sheet.getRows();i++){
				Book bo=new Book();
				Cell cell=sheet.getCell(0,i);
				bo.setId(Integer.valueOf(cell.getContents()));
				bo.setName(sheet.getCell(1,i).getContents());
				bo.setType(sheet.getCell(2,0).getContents());
				ar.add(bo);
				
			}
		} catch (BiffException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			book.close();
		}
		return ar;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ExcelDemo01 eb=new ExcelDemo01();
	
		ArrayList<Book> ar1=eb.excleIn();
		for(Book bo2:ar1){
			System.out.println(bo2.getName()+bo2.getType());
		}
	}

}

212656_Ndc7_2356966.png

 利用反射实现通用 Excel 导入、导出

转载于:https://my.oschina.net/u/2356966/blog/409174

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值