Java简单类型判断

package com;

/**
 * Java类型判断,工具类
 * 
 * @author LinXu
 *
 */
public class TypeIns {
    public static void main(String[] args) {
        Object object = 9;// Object类
        System.out.println("是否是String:" + TypeConversion.isString(object));// 判断是否是|String类型
        System.out.println("是否是Int:" + TypeConversion.isInt(object));
        System.out.println("获取当前Object类型:" + TypeConversion.getClassType(object));
        Class<?> clazz = TypeConversion.getClassType(object);
        if (TypeConversion.isInt(clazz)) {// 判断是否是int
            System.out.println("是Integer类型");
        } else if (TypeConversion.isString(object)) {// 判断是否是string
            System.out.println("是String类型");

        }

    }
}

class TypeConversion<T> {

    public static <T> boolean isString(T t) {
        return t instanceof String;
    }

    public static <T> boolean isByte(T t) {
        return t instanceof Byte;
    }

    public static <T> boolean isShort(T t) {
        return t instanceof Short;
    }

    public static <T> boolean isInt(T t) {
        return t instanceof Integer;
    }

    public static <T> boolean isLong(T t) {
        return t instanceof Long;
    }

    public static <T> boolean isChar(T t) {
        return t instanceof Character;
    }

    public static <T> boolean isFloat(T t) {
        return t instanceof Float;
    }

    public static <T> boolean isDouble(T t) {
        return t instanceof Double;
    }

    public static <T> boolean isBytes(T t) {
        return t instanceof Byte;

    }

    public static <T> Class<?> getClassType(T t) {
        return t.getClass();

    }
}



是否是String:false
是否是Int:true
获取当前Object类型:class java.lang.Integer

 

posted on 2019-02-01 12:13  Mature1021  阅读(...)  评论(...编辑  收藏

统计