将数字对象自动转换为它们的本机
Automatic conversion of numeric objects to their natives
我正在尝试创建一个函数,该函数将在给定一组参数的情况下调用 class 的构造函数
包测试测试;
import java.lang.reflect.Constructor;
public class MainClass {
public static <T> T newClass(Class<?> inst, Object ... args){
@SuppressWarnings("unchecked")
Constructor<?> [] ctor = (inst.getDeclaredConstructors());
int argIndex = 0;
ctorLoop: for(Constructor<?> x : ctor){
argIndex = 0;
for(Class<?> s : x.getParameterTypes()){
if(argIndex > args.length || args[argIndex++].getClass() != s){
if(argIndex <= args.length)
System.out.println("Param doesnt match : " + args[argIndex-1].getClass() + " with " + s);
continue ctorLoop;
}
}
try{
return (T)x.newInstance(args);
}catch(Exception e){
System.err.println("Error in instantiating instance of class : " + inst);
return null;
}
}
System.err.println("No instance of constructor found for class " + inst);
return null;
}
public static void main(String[] args) {
System.out.println(newClass(Double.class,5.0));
}
}
这给了我错误
Param doesnt match : class java.lang.Double with double
Param doesnt match : class java.lang.Double with class java.lang.String
No instance of constructor found for class class java.lang.Double
看线
Param doesnt match : class java.lang.Double with double
有没有一种方法可以在不交换每个本机类型(double、float、long、int 等)的情况下本机进行此布尔匹配?
在包装器 类 中定义了代表基本类型的 Class
对象的常量。对于 double
,使用 Double.TYPE
.
The Class
instance representing the primitive type double
.
这应该与您正在寻找的构造函数的假定 double
参数匹配。
其他基元的其他示例是 Integer.TYPE
、Float.TYPE
、Short.TYPE
、Byte.TYPE
、Long.TYPE
、Character.TYPE
和 Boolean.TYPE
。 void
.
甚至还有 Void.TYPE
不,没有。引用类型java.lang.Double
和原始类型double
都有对应的Class
对象。无法从一个 Class
对象转到另一个对象 (boxing/unboxing)。
您必须保留自己的 (bi)map。
最简单的方法是使用 java.beans.Statement
。它会自动为您处理所有这些转换。
我正在尝试创建一个函数,该函数将在给定一组参数的情况下调用 class 的构造函数 包测试测试;
import java.lang.reflect.Constructor;
public class MainClass {
public static <T> T newClass(Class<?> inst, Object ... args){
@SuppressWarnings("unchecked")
Constructor<?> [] ctor = (inst.getDeclaredConstructors());
int argIndex = 0;
ctorLoop: for(Constructor<?> x : ctor){
argIndex = 0;
for(Class<?> s : x.getParameterTypes()){
if(argIndex > args.length || args[argIndex++].getClass() != s){
if(argIndex <= args.length)
System.out.println("Param doesnt match : " + args[argIndex-1].getClass() + " with " + s);
continue ctorLoop;
}
}
try{
return (T)x.newInstance(args);
}catch(Exception e){
System.err.println("Error in instantiating instance of class : " + inst);
return null;
}
}
System.err.println("No instance of constructor found for class " + inst);
return null;
}
public static void main(String[] args) {
System.out.println(newClass(Double.class,5.0));
}
}
这给了我错误
Param doesnt match : class java.lang.Double with double
Param doesnt match : class java.lang.Double with class java.lang.String
No instance of constructor found for class class java.lang.Double
看线
Param doesnt match : class java.lang.Double with double
有没有一种方法可以在不交换每个本机类型(double、float、long、int 等)的情况下本机进行此布尔匹配?
在包装器 类 中定义了代表基本类型的 Class
对象的常量。对于 double
,使用 Double.TYPE
.
The
Class
instance representing the primitive typedouble
.
这应该与您正在寻找的构造函数的假定 double
参数匹配。
其他基元的其他示例是 Integer.TYPE
、Float.TYPE
、Short.TYPE
、Byte.TYPE
、Long.TYPE
、Character.TYPE
和 Boolean.TYPE
。 void
.
Void.TYPE
不,没有。引用类型java.lang.Double
和原始类型double
都有对应的Class
对象。无法从一个 Class
对象转到另一个对象 (boxing/unboxing)。
您必须保留自己的 (bi)map。
最简单的方法是使用 java.beans.Statement
。它会自动为您处理所有这些转换。