Java变量类型识别的3种方式

本文介绍了一个自定义封装的类型识别工具类,可以识别多种基本数据类型和引用类型,并提供其类型和描述。

转载一篇类型识别的文章,亮点是通过多态自己封装的一个识别公用类。如要查具体内容,请查看原文:

http://snkcxy.iteye.com/blog/1827913

package com.cxyapi.oo;  
  
import java.util.HashMap;  
import java.util.Map;  
  
/** 类型识别工具 
 * @author cxy @ www.cxyapi.com 
 */  
public class TypeTools  
{  
    //获得类型  
    public static Map<String,String> getType(Object o)  
    {  
        Map<String,String> typeInfo=new HashMap<String,String>();  
        typeInfo.put("类型", o.getClass().getSimpleName());  
        typeInfo.put("描述", "引用类型");  
        return typeInfo;  
    }  
      
    public static Map<String,String> getType(int i)  
    {  
        Map<String,String> typeInfo=new HashMap<String,String>();  
        typeInfo.put("类型", "int");  
        typeInfo.put("描述", "整形");  
        return typeInfo;  
    }  
      
    public static Map<String,String> getType(long l)  
    {  
        Map<String,String> typeInfo=new HashMap<String,String>();  
        typeInfo.put("类型", "long");  
        typeInfo.put("描述", "长整型");  
        return typeInfo;  
    }  
      
    public static Map<String,String> getType(boolean b)  
    {  
        Map<String,String> typeInfo=new HashMap<String,String>();  
        typeInfo.put("类型", "boolean");  
        typeInfo.put("描述", "布尔类型");  
        return typeInfo;  
    }  
      
    public static Map<String,String> getType(char b)  
    {  
        Map<String,String> typeInfo=new HashMap<String,String>();  
        typeInfo.put("类型", "char");  
        typeInfo.put("描述", "字符");  
        return typeInfo;  
    }  
      
    public static Map<String,String> getType(float f)  
    {  
        Map<String,String> typeInfo=new HashMap<String,String>();  
        typeInfo.put("类型", "float");  
        typeInfo.put("描述", "单精度浮点型");  
        return typeInfo;  
    }  
      
    public static Map<String,String> getType(double d)  
    {  
        Map<String,String> typeInfo=new HashMap<String,String>();  
        typeInfo.put("类型", "double");  
        typeInfo.put("描述", "双精度浮点型");  
        return typeInfo;  
    }  
      
    public static Map<String,String> getType(String s)  
    {  
        Map<String,String> typeInfo=new HashMap<String,String>();  
        typeInfo.put("类型", "String");  
        typeInfo.put("描述", "字符串类型");  
        return typeInfo;  
    }  
      
}  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值