论坛首页 Java版 企业应用

Class.isInstance(obj) 和 obj instanceof Class 的区别?

浏览 592 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
最后更新时间:2008-03-13
代码如下:
public static List findObjectByOther(Class theClass, Field field, Object value){
		List objs = new ArrayList();
		Field[] fields = null;
		boolean isAField = false;
		boolean isTheType = false;
		
		if(theClass != null){
			fields = theClass.getDeclaredFields();
		}
		
		if(field != null){
			isTheType = field.getType().isInstance(value);	//假如使用 value instanceof field.getTypr(),则提示field.getTypr() 不是一个class,找不到原因所在,请大伙指教.
		}
		
		if(fields != null){
			for(int i=0; i<fields.length; i++){
				if(field.equals(fields[i])){
					isAField = true;
					break;
				}
			}
		}
		
		if(isAField && isTheType){
			String hql = "from " + theClass.getName() + " where " + field.getName() + "=:value";
			try {
				HibernateSessionFactory.currentSession().beginTransaction();
				objs = HibernateSessionFactory.currentSession().createQuery(hql).setParameter("value", value).list();
			} catch (HibernateException e) {
				log.error(e);
			} catch (SessionInitialException e) {
				log.error(e);
			}
		}
		return objs;
	}
   
最后更新时间:2008-03-13
instanceof左边是对象,右边是类型。
field.getTypr()返回的是Class类型的对象。
   
0 请登录后投票
最后更新时间:2008-07-24
倒是对你的查询感兴趣
   
0 请登录后投票
论坛首页 Java版 企业应用

跳转论坛:
JavaEye推荐