当前位置: 代码迷 >> Java Web开发 >> 这边为什么这样用呢?
  详细解决方案

这边为什么这样用呢?

热度:9118   发布时间:2016-04-11 00:04:33.0
这里为什么这样用呢???

package cn.itcast.utils;

import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

public class BeanHandler implements ResultSetHandler {
private Class clazz;
public BeanHandler(Class clazz){
this.clazz = clazz;
}
public Object handler(ResultSet rs) {
try{
if(!rs.next()){
return null;
}
Object bean = clazz.newInstance();

ResultSetMetaData metadata = rs.getMetaData();
int columnCount = metadata.getColumnCount();  //得到结果集中有几列数据
for(int i=0;i<columnCount;i++){
String coulmnName = metadata.getColumnName(i+1);  //得到每列的列名
Object coulmnData = rs.getObject(i+1);

Field f = clazz.getDeclaredField(coulmnName);//反射出类上列名对应的属性
f.setAccessible(true);
f.set(bean, coulmnData);
}
return bean;


}catch (Exception e) {
throw new RuntimeException(e);
}
}
}



这里为什么用getDeclaredField()方法,getDeclaredField()方法不是获取私有字段的吗?但是这里它是怎么确定clazz类中的字段都是私有的,或者是getDeclaredField()方法既可以获得私有字段,也可以获得公有字段吗?查jdk api文档就没发现介绍这个方法的详细使用情况,大家都查什么文档呢?
jdk api 文档

------解决方案--------------------
引用:

package cn.itcast.utils;

import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

public class BeanHandler implements ResultSetHandler {
private Class clazz;
public BeanHandler(Class clazz){
this.clazz = clazz;
}
public Object handler(ResultSet rs) {
try{
if(!rs.next()){
return null;
}
Object bean = clazz.newInstance();

ResultSetMetaData metadata = rs.getMetaData();
int columnCount = metadata.getColumnCount();  //得到结果集中有几列数据
for(int i=0;i<columnCount;i++){
String coulmnName = metadata.getColumnName(i+1);  //得到每列的列名
Object coulmnData = rs.getObject(i+1);

Field f = clazz.getDeclaredField(coulmnName);//反射出类上列名对应的属性
f.setAccessible(true);
f.set(bean, coulmnData);
}
return bean;


}catch (Exception e) {
throw new RuntimeException(e);
}
}
}



这里为什么用getDeclaredField()方法,getDeclaredField()方法不是获取私有字段的吗?但是这里它是怎么确定clazz类中的字段都是私有的,或者是getDeclaredField()方法既可以获得私有字段,也可以获得公有字段吗?查jdk api文档就没发现介绍这个方法的详细使用情况,大家都查什么文档呢?

getDeclaredField是获取已声明的字段
------解决方案--------------------
这个方法返回的是所有类型的,public protected default private

看这里:

    getDeclaredFields

    public Field[] getDeclaredFields()
                              throws SecurityException

    Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object. This includes public, protected, default (package) access, and private fields, but excludes inherited fields. The elements in the array returned are not sorted and are not in any particular order. This method returns an array of length 0 if the class or interface declares no fields, or if this Class object represents a primitive type, an array class, or void.

    See The Java Language Specification, sections 8.2 and 8.3.

    Returns:
        the array of Field objects representing all the declared fields of this class
    Throws:
        SecurityException - If a security manager, s, is present and any of the following conditions is met:
            invocation of s.checkMemberAccess(this, Member.DECLARED) denies access to the declared fields within this class
  相关解决方案