public void test(Object o,String typeName)
{
//在这里如何将o转为指定 String 或 int 或double 类型
//从而可动态去调用 checkValueOK() 来适应。
// 不想用 (String)o (int)o 转换的方法,想通过typeName 类名来动态将object转为typeName指定的类型
// typeName 传入的值类似 "java.lang.String" "int" "double"
} /**校验值为有效值*/
public static boolean checkValueOK(String value)
{
return value != null;
}
public static boolean checkValueOK(int value)
{
return value != -Integer.MAX_VALUE;
}
public static boolean checkValueOK(double value)
{
return value != -Double.MAX_VALUE;
}
{
//在这里如何将o转为指定 String 或 int 或double 类型
//从而可动态去调用 checkValueOK() 来适应。
// 不想用 (String)o (int)o 转换的方法,想通过typeName 类名来动态将object转为typeName指定的类型
// typeName 传入的值类似 "java.lang.String" "int" "double"
} /**校验值为有效值*/
public static boolean checkValueOK(String value)
{
return value != null;
}
public static boolean checkValueOK(int value)
{
return value != -Integer.MAX_VALUE;
}
public static boolean checkValueOK(double value)
{
return value != -Double.MAX_VALUE;
}
解决方案 »
- java小问题
- java如何实现telnet功能?
- [100分]很基础的问题:关于异常处理try{}catch(){}、throws和throw用法和区别的问题,下面的方法get(.),get1(.),get2(.),get3(.)关于
- 如何正确实现List 和 Vector 之间的互相转换?
- nextInt在JAVA里是什么东西呀,是不是取下一下整数的意思呀?
- 感兴趣的近来闲聊一下Delphi跟Java的比较!!!!进者有分
- 多线程学习笔记
- 在命令行输入appletviewer Run1.java,为什么没有显示??
- 我是入门者,三层开发方式具体是指那三层,各层又作什么用的,B/S是否可称得上是三层开发方式呢?
- 是java阵营的就看过来!我如何在IBM RS/6000小型机上运行我写的java程序?
- 在Java Swing低迷的今天,放出一针强心剂,希望能有更多的朋友研究Swing...
- 为什么移用java -cp的参数,就运行不起来了?,诡异啊,麻烦大家了
s表示类型名,比如s="java.lang.String";
可以参考一下下面的,如果楼主觉得合适就用,public static boolean checkValueOK(Object value)
{
try
{
if (Class.forName("java.lang.Double").isInstance(value))
{
return ((Double)value).doubleValue() != -Double.MAX_VALUE;
}
else if (Class.forName("java.lang.Integer").isInstance(value))
{
return ((Integer)value).intValue() != -Integer.MAX_VALUE;
}
else
{
return value != null;
}
}
catch (ClassNotFoundException nex)
{
}
return false;
}
public class Test{
public static boolean test(Object o){
if(o instanceof String){
return (String)o!= null;
}
if(o instanceof Integer){
return (Integer)o!= -Integer.MAX_VALUE;
}
if(o instanceof Double ){
return (Double)o != -Double.MAX_VALUE;
}
return true;
}
public static void main(String[] args){
System.out.println(test("s"));
System.out.println(test(new Integer(5)));
System.out.println(test(new Double(4d)));
}
}
我来改一下public static boolean test(Object o)
{
if(o instanceof String){
return (String)o!= null;
}
if(o instanceof Integer){
return ((Integer)o).intValue()!= -Integer.MAX_VALUE;
}
if(o instanceof Double ){
return ((Double)o).doubleValue() != -Double.MAX_VALUE;
}
return true;
}
if (typeName.equals("string))
{
(String)o;
}
else if (typeName.equals("int"))
{ (Integer)o;
}
你的思路好像不合适吧?如果你的方法可以运行的话:test (猫的实例,狗的类型);
把猫变成狗也就成立了.小心 ClassCastException 满天飞。
public class Test
{
public static void main(String[] args)
{
try
{
new Test().test("test","java.lang.String");
new Test().test(new Integer(44),"java.lang.Integer");
new Test().test(new Double(44.0d),"java.lang.Double");
}
catch(Exception e){e.printStackTrace();}
}
public void test(Object o,String typeName) throws ClassNotFoundException,IllegalAccessException,InvocationTargetException
{
Object rt=null;
Class c=Class.forName(typeName);
Class tc=this.getClass();
Method[] m=tc.getDeclaredMethods();
for(int i=0;i<m.length;i++)
{
//System.out.println(m[i].toString());
Class[] mc=m[i].getParameterTypes();
if(mc[0]==c)
{
rt=m[i].invoke(this,o);
System.out.println("invoke "+m[i].toString());
}
}
} /**校验值为有效值*/
public static boolean checkValueOK(String value)
{
return value != null;
}
public static boolean checkValueOK(Integer value) //为了实现,我把原始类型改为了包装类型!!! {
return value.intValue()!= -Integer.MAX_VALUE;
}
public static boolean checkValueOK(Double value)
{
return value.doubleValue()!= -Double.MAX_VALUE;
}
}
//在containerTypes 中存储所有可能进行转换的类型.
final Class[] containerTypes = { String.class, Integer.class, Double.class};
void test(Object o,String typeName) {
for (int j = 0; j < containerTypes.length; ++j) {
if (containerTypes[j].isInstance(o)) {
switch (j){
case 0: checkValueOK((String)o );break;
case 1: checkValueOK((Integer)o) ; break;
case 2: checkValueOK((Double)o) ; break;
}
break;
}
}
}