public static Class<?> forName(String className)是Class类的一个static成员。其返回是一个Class对象的引用,该引用对应着传入的className。如果该Product还未被加载就加载它,在加载类Product时,Product的static{}会被执行。如果找不到要加载的类,则会抛出异常ClassNotFoundException。
publicclassReflectionTest { publicstaticvoidmain(String[] args) { // read class name from command line args or user input String name; if (args.length > 0) name = args[0]; else { Scanner in = new Scanner(System.in); System.out.println("Enter class name (e.g. java.util.Date): "); name = in.next(); }
try { // print class name and superclass name (if != Object) Class cl = Class.forName(name); Class supercl = cl.getSuperclass(); String modifiers = Modifier.toString(cl.getModifiers()); if (modifiers.length() > 0) System.out.print(modifiers + " "); System.out.print("class " + name); if (supercl != null && supercl != Object.class) System.out.print(" extends " + supercl.getName());
/** * Prints all constructors of a class * @param cl a class */ publicstaticvoidprintConstructors(Class cl) { Constructor[] constructors = cl.getDeclaredConstructors();//获取所有构造方法,如果方法中没有Declared,则不能获取私有构造方法
for (Constructor c : constructors) { String name = c.getName(); System.out.print(" "); String modifiers = Modifier.toString(c.getModifiers()); if (modifiers.length() > 0) System.out.print(modifiers + " "); System.out.print(name + "(");
/** * Prints all methods of a class * @param cl a class */ publicstaticvoidprintMethods(Class cl) { Method[] methods = cl.getDeclaredMethods();//获取这个类/接口的所有方法
for (Method m : methods) { Class retType = m.getReturnType();//获取返回值 String name = m.getName();//获取方法名
System.out.print(" "); // print modifiers, return type and method name String modifiers = Modifier.toString(m.getModifiers());//获取方法修饰符 if (modifiers.length() > 0) System.out.print(modifiers + " "); System.out.print(retType.getName() + " " + name + "(");
publicclassCopyOfTest { publicstaticvoidmain(String[] args) { int[] a = { 1, 2, 3 }; a = (int[]) goodCopyOf(a, 10); System.out.println(Arrays.toString(a));
String[] b = { "Tom", "Dick", "Harry" }; b = (String[]) goodCopyOf(b, 10); System.out.println(Arrays.toString(b));
System.out.println("The following call will generate an exception."); b = (String[]) badCopyOf(b, 10); }
/** * This method attempts to grow an array by allocating a new array and copying all elements. * @param a the array to grow * @param newLength the new length * @return a larger array that contains all elements of a. However, the returned array has * type Object[], not the same type as a */ publicstatic Object[] badCopyOf(Object[] a, int newLength) // not useful //一个从一开始就是Object[]的数组是不能够向下转型为其它类型数组的,故需要在一开始创建与原数组类型相同的数组 { Object[] newArray = new Object[newLength]; System.arraycopy(a, 0, newArray, 0, Math.min(a.length, newLength)); return newArray; }
/** * This method grows an array by allocating a new array of the same type and * copying all elements. * @param a the array to grow. This can be an object array or a primitive * type array * @return a larger array that contains all elements of a. */ publicstatic Object goodCopyOf(Object a, int newLength) { Class cl = a.getClass(); if (!cl.isArray()) returnnull; Class componentType = cl.getComponentType();//获取数据类型 int length = Array.getLength(a); Object newArray = Array.newInstance(componentType, newLength); System.arraycopy(a, 0, newArray, 0, Math.min(length, newLength)); return newArray; } }
/** * Returns the square of a number * @param x a number * @return x squared */ publicstaticdoublesquare(double x) { return x * x; }
/** * Prints a table with x- and y-values for a method * @param from the lower bound for the x-values * @param to the upper bound for the x-values * @param n the number of rows in the table * @param f a method with a double parameter and double return value */ publicstaticvoidprintTable(double from, double to, int n, Method f) { // print out the method as table header System.out.println(f);
double dx = (to - from) / (n - 1);
for (double x = from; x <= to; x += dx) { try { double y = (Double) f.invoke(null, x);//调用当前封装在Method中的方法,如果是静态方法,第一个参数隐式参数传null,后面若干变量为方法参数。 System.out.printf("%10.4f | %10.4f%n", x, y); } catch (Exception e) { e.printStackTrace(); } } } }