用class对象来执行类型转换和直接强制类型转换有什么区别?
比如Building b=new house();
    class<house> housetype=house.class;
    house h=housetype.cast(b);
    h=(house)b;
请问后两句有什么区别?
能否举例说下用class对象来执行类型转换和直接强制类型转换有什么区别? 
什么情况下必须使用class对象进行类型转换而不能直接强制类型转换?

解决方案 »

  1.   


    /**
         * Casts an object to the class or interface represented
         * by this <tt>Class</tt> object.
         *
         * @param obj the object to be cast
         * @return the object after casting, or null if obj is null
         *
         * @throws ClassCastException if the object is not
         * null and is not assignable to the type T.
         *
         * @since 1.5
         */
        public T cast(Object obj) {
    if (obj != null && !isInstance(obj))
        throw new ClassCastException();
    return (T) obj;
        }
    其实没多大区别。。
      

  2.   

    public <T> T cast(Object o, Class<T> clz){
    return clz.cast(o);
    }
    这种情况,你就只能用.cast
      

  3.   

    public <T> T cast(Object o, Class<T> clz){
        return (T)o;
    }
    怎么破?
      

  4.   

    public <T> T cast(Object o, Class<T> clz){
        return (T)o;
    }
    怎么破?
    有道理,我破了