反射机制
通过Class对象获取类的成员的行为通常来说就是反射
1.由于Class类没有公共构造方法,所有创建Class的对象的方法有以下几种:
①通过Class.forName()静态方法返回Class类的一个实例
Class cls = Class.forName("cn.gzsxt.test.Dog");
②通过类名.class来获取一个Class类的实例
cls = Dog.class;
③代码运行在非静态区,通过this.getClass()获取Class类的一个实例
Class cls = this.getClass();
2.Class对象既然是描述一个类,那么我们通过该Class的实例能够做什么事情呢?
获取类的属性:
①获取类名
System.out.println(cls.getName());
System.out.println(cls.getSimpleName());
②获取类的修饰限定符
int modi = cls.getModifiers();
System.out.println(modi);
③根据一个属性名获取一个public属性的描述
Field field = cls.getField("name");
④获取一个类的全部public属性的描述
Field[] fields = cls.getFields();
System.out.println(fields.length);
⑤根据一个属性名获取一个在类中声明的属性的描述
Field declaredField = cls.getDeclaredField("gender");
⑥获取所有在类中声明的属性的描述
Field[] declaredFields = cls.getDeclaredFields();
⑦获取一个类的方法
根据一个方法的名字获取类中的一个public的方法的描述
Method method = cls.getMethod("run", String.class,int.class);
⑧获取所有在类中是public的方法的描述
Method[] methods = cls.getMethods();
⑨根据一个方法名获取在类中声明的一个方法的描述
Method declaredMethod = cls.getDeclaredMethod("eat", null);
获取所有在类中声明的方法的描述:
Method[] declaredMethods = cls.getDeclaredMethods();
返回cls所代表的类的实例:
Object obj = cls.newInstance();
System.out.println(obj);
通过一个例子来体现反射机制:
例:通过java语句查询oracle数据库中的emp表的全部信息
①连接数据库
package cn.sxt.util;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class DBUtil {
private static String DRIVER = "";
private static String URL = "";
private static String USER = "";
private static String PASSWORD = "";
static {
try {
InputStream in = Thread .currentThread().getContextClassLoader().getResourceAsStream("db.properties");
Properties prop = new Properties();
prop.load(in);
DRIVER = prop.getProperty("driver");
URL = prop.getProperty("url");
USER = prop.getProperty("user");
PASSWORD = prop.getProperty("password");
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
Connection conn = null;
try {
conn = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void close(Connection conn,Statement ps) {
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(Connection conn,Statement ps,ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
②将Emp封装:
package cn.sxt.model;
import java.util.Date;
public class Emp {
private int empno;
private String ename;
private String job;
private int mgr;
private Date hiredate;
private double sal;
private double comm;
private int deptno;
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getMgr() {
return mgr;
}
public void setMgr(int mgr) {
this.mgr = mgr;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
public double getComm() {
return comm;
}
public void setComm(double comm) {
this.comm = comm;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public Emp() {
super();
}
public Emp(int empno, String ename, String job, int mgr, Date hiredate, double sal, double comm, int deptno) {
super();
this.empno = empno;
this.ename = ename;
this.job = job;
this.mgr = mgr;
this.hiredate = hiredate;
this.sal = sal;
this.comm = comm;
this.deptno = deptno;
}
@Override
public String toString() {
return "Emp [empno=" + empno + ", ename=" + ename + ", job=" + job + ", mgr=" + mgr + ", hiredate=" + hiredate
+ ", sal=" + sal + ", comm=" + comm + ", deptno=" + deptno + "]";
}
}
③创建test类
package cn.sxt.test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import cn.sxt.model.Emp;
import cn.sxt.util.DBUtil;
public class TestEmp {
public static void main(String[] args) {
TestEmp te = new TestEmp();
List list = te.getAllEmp();
}
public List getAllEmp() {
List list = new ArrayList();
Connection conn = DBUtil.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
try {
//写sql语句
String sql = "select * from emp";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
Emp e = new Emp();
e.setEmpno(rs.getInt("empno"));
e.setEname(rs.getString("ename"));
e.setJob(rs.getString("job"));
e.setMgr(rs.getInt("mgr"));
e.setHiredate(rs.getDate("hiredate"));
e.setSal(rs.getDouble("comm"));
e.setDeptno(rs.getInt("deptno"));
list.add(e);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.close(conn, ps, rs);
}
return list;
}
}