Java变量类型识别的3种方式
2013-03-14 09:02
435 查看
变量类型识别有3种方法:
通过反射拿到变量的类型
instanceof关键字判断
通过java的多态(方法重载)来DIY类型识别
具体看例子吧,尤其第三种方式 有一个自定义的TypeTools,我觉得很实用。希望大家喜欢~
Java代码
package com.cxyapi.oo;
/** 类型识别工具测试类
* @author cxy @ www.cxyapi.com
*/
public class TypeToolsTest
{
public static void main(String[] args)
{
int i=0;
TypeObject to=new TypeObject();
//1.反射
System.out.println("to的类型:"+to.getClass().getSimpleName());
System.out.println(int.class.getSimpleName());
System.out.println(Integer.class.getSimpleName());
//但是对于一个不确定类型的基本数据类型变量我们没法用反射来获取其类型。
System.out.println("----------------------");
//2.instanceof
if(to instanceof TypeObject){ System.out.println("to是TypeObject类型的");}
//但是这种办法貌似也没法确定基本数据类型
System.out.println("----------------------");
//以上两种方式对于对象,引用类型的都很好用,但是对基本数据类型就不那么好用了。
//3.通过多态(方法的重载)
System.out.println("i是:"+TypeTools.getType(i));
System.out.println("to是:"+TypeTools.getType(to));
System.out.println("\"cxyapi\"是:"+TypeTools.getType("www.cxyapi.com"));
//大家可以看出来 最后一种方式使用多态的方式达到了检测类型(基本类型和引用类型)的目的
//它除了弥补其他两种方式不能检测基本数据类型的不足在外,还能自己DIY类型信息
}
}
//定义一个类,为了演示引用类型的类型检测
class TypeObject{}
自定义的类型识别工具:
Java代码
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;
}
}
通过反射拿到变量的类型
instanceof关键字判断
通过java的多态(方法重载)来DIY类型识别
具体看例子吧,尤其第三种方式 有一个自定义的TypeTools,我觉得很实用。希望大家喜欢~
Java代码
package com.cxyapi.oo;
/** 类型识别工具测试类
* @author cxy @ www.cxyapi.com
*/
public class TypeToolsTest
{
public static void main(String[] args)
{
int i=0;
TypeObject to=new TypeObject();
//1.反射
System.out.println("to的类型:"+to.getClass().getSimpleName());
System.out.println(int.class.getSimpleName());
System.out.println(Integer.class.getSimpleName());
//但是对于一个不确定类型的基本数据类型变量我们没法用反射来获取其类型。
System.out.println("----------------------");
//2.instanceof
if(to instanceof TypeObject){ System.out.println("to是TypeObject类型的");}
//但是这种办法貌似也没法确定基本数据类型
System.out.println("----------------------");
//以上两种方式对于对象,引用类型的都很好用,但是对基本数据类型就不那么好用了。
//3.通过多态(方法的重载)
System.out.println("i是:"+TypeTools.getType(i));
System.out.println("to是:"+TypeTools.getType(to));
System.out.println("\"cxyapi\"是:"+TypeTools.getType("www.cxyapi.com"));
//大家可以看出来 最后一种方式使用多态的方式达到了检测类型(基本类型和引用类型)的目的
//它除了弥补其他两种方式不能检测基本数据类型的不足在外,还能自己DIY类型信息
}
}
//定义一个类,为了演示引用类型的类型检测
class TypeObject{}
package com.cxyapi.oo; /** 类型识别工具测试类 * @author cxy @ www.cxyapi.com */ public class TypeToolsTest { public static void main(String[] args) { int i=0; TypeObject to=new TypeObject(); //1.反射 System.out.println("to的类型:"+to.getClass().getSimpleName()); System.out.println(int.class.getSimpleName()); System.out.println(Integer.class.getSimpleName()); //但是对于一个不确定类型的基本数据类型变量我们没法用反射来获取其类型。 System.out.println("----------------------"); //2.instanceof if(to instanceof TypeObject){ System.out.println("to是TypeObject类型的");} //但是这种办法貌似也没法确定基本数据类型 System.out.println("----------------------"); //以上两种方式对于对象,引用类型的都很好用,但是对基本数据类型就不那么好用了。 //3.通过多态(方法的重载) System.out.println("i是:"+TypeTools.getType(i)); System.out.println("to是:"+TypeTools.getType(to)); System.out.println("\"cxyapi\"是:"+TypeTools.getType("www.cxyapi.com")); //大家可以看出来 最后一种方式使用多态的方式达到了检测类型(基本类型和引用类型)的目的 //它除了弥补其他两种方式不能检测基本数据类型的不足在外,还能自己DIY类型信息 } } //定义一个类,为了演示引用类型的类型检测 class TypeObject{}
自定义的类型识别工具:
Java代码
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;
}
}
相关文章推荐
- Java变量类型识别的3种方式
- JavaSE20_总结之Java变量类型识别的方式
- Java的String.valueOf 转换 与、空串+类型变量转换与封装类(Integer)的toString方式转换比较。
- 在java中对byte类型的变量的运算方式
- 大数据Java-交换变量的3种方式
- 用类名做方法的返回值类型 在学习Java的初始阶段,很多同学使用基本数据类型定义变量和引用类型定义变量以及使用类名做方法的返回值类型常常不知所以。今天我以自己的解读方式和个人的理解作一个简单的解释,和
- Java自学之路-Java基础教程-9:Java的变量类型-8种基本类型和3种引用类型
- C++中RTTI动态类型识别、静态成员、变量的四种存储方式
- Java中变量的说明,常量的表示方式,Java数据类型,个别运算符的说明
- Java的String.valueOf 转换 与、空串+类型变量转换与封装类(Integer)的toString方式转换比较
- java中的局部变量、成员变量以及数据类型
- JAVA 基础笔记之一 char类型变量能否保存一个汉字
- java变量、二进制、数据类型、原码、补码、反码
- java变量和数据类型
- java语言基础(7)——数据类型、变量的定义和使用
- JAVA程序员学python-1.变量及数据类型
- C语言之变量类型和存储方式
- java 变量的内存空间及数据类型
- 2 Java基础语法(关键字,标识符,注释,常量,进制转换,变量,数据类型,数据类型转换)
- java中基本数据类型与引用类型比较方式