源代码如下:显示命令行中类的构造函数
import java.lang.reflect.*;public class ShowMethods {
static final String usage ="usage: \n" ;
public static void main(String[] args) {
if(args.length < 1) {
System.out.println(usage);
System.exit(0);
}
try {
Class c = Class.forName(args[0]);
//Constructor[] ctor = c.getConstructors();不会出错
Constructor[] ctor=c.getConstructor(new Class[]{byte.class});//有错,为什么?
if(args.length == 1) {
for (int i = 0; i < ctor.length; i++)
System.out.println(ctor[i]);
} else {
for (int i = 0; i < ctor.length; i++)
if(ctor[i].toString()
.indexOf(args[1])!= -1)
System.out.println(ctor[i]);
}
} catch(ClassNotFoundException e) {
System.err.println("No such class: " + e);
}
}
} ///:~
import java.lang.reflect.*;public class ShowMethods {
static final String usage ="usage: \n" ;
public static void main(String[] args) {
if(args.length < 1) {
System.out.println(usage);
System.exit(0);
}
try {
Class c = Class.forName(args[0]);
//Constructor[] ctor = c.getConstructors();不会出错
Constructor[] ctor=c.getConstructor(new Class[]{byte.class});//有错,为什么?
if(args.length == 1) {
for (int i = 0; i < ctor.length; i++)
System.out.println(ctor[i]);
} else {
for (int i = 0; i < ctor.length; i++)
if(ctor[i].toString()
.indexOf(args[1])!= -1)
System.out.println(ctor[i]);
}
} catch(ClassNotFoundException e) {
System.err.println("No such class: " + e);
}
}
} ///:~
为什么出错?请看:
getConstructor
public Constructor getConstructor(Class[] parameterTypes)
throws NoSuchMethodException,
SecurityException
该函数返回是什么?是个Constructor。而你赋值成了Constructor[],你说它能通过吗?