请参见下面的代码,并说我什么是输精管。爪哇 - 声明类和类对象之间的差异和(级VS级)
public class c1
{
//....
}
Class c2 = obj.getClass();
Object o1 = new c1();
Object o2 = new c2(); // <<----- here
什么错了最后一行???
我怎样才能创建C2的对象????
编辑:
int[] s1 = new int[]{4,5,6};
char[] s2 = new char[]{'a'.'b'};
Integer[] new = convertPrimitiveArrayToObject(new Object[]{s1});
Character[] new = convertPrimitiveArrayToObject(new Object[]{s2});
public static <T> T[] convertPrimitiveArrayToObject(Object[] primitive)
{
Object x = primitive[0];
Class type = x.getClass().getComponentType(); // => int OR char
type[] x2 = (type[]) x; // I need to convert array to (int/char/...)
//......
}
你期待能够为'新c2'后你'obj.getClass()'? – ChiefTwoPencils
'c2'不是班级。它是Class类型的一个对象。 – Batty
对于参数'Object [] primitive',您不能传递任何'int []'或'char []'数组。也许你应该提供一个你想要实现的例子。 – SubOptimal