Java将Object类型的对象转为指定的类型

    public static <T> T cast(Object o, Class<T> type) {
    
    
        if (type.isInstance(o)) return type.cast(o);
        throw new RuntimeException("can not cast " + o.getClass() + " to '" + type);
    }
    

猜你喜欢

转载自blog.csdn.net/daiyi666/article/details/120107673