java又一摘要Reflection
Only inner classes can be private.
Regular classes always have either package or public visibility.
我们一直强调网站制作、成都做网站对于企业的重要性,如果您也觉得重要,那么就需要我们慎重对待,选择一个安全靠谱的网站建设公司,企业网站我们建议是要么不做,要么就做好,让网站能真正成为企业发展过程中的有力推手。专业网络公司不一定是大公司,创新互联作为专业的网络公司选择我们就是放心。
inner class:If an inner class has constructors, the compiler modifies them,
adding a parameter for the outer class reference.
An inner class method gets to access both its own data fields
and those of the outer object creating it.
inner class -> local inner class -> anonymous inner class
Whenever you would use a function pointer in C++,
you should consider using an interface in Java.
we suggest that you use Method objects in your own programs
only when absolutely necessary. Using interfaces and inner
classes is almost always a better idea. In particular, we echo
the developers of Java and suggest not using Method objects
for callback functions. Using interfaces for the callbacks
leads to code that runs faster and is a lot more maintainable.
=====================================
Using Reflection to Analyze the Capabilities of Classes
Example 5-5. ReflectionTest.java
1. import java.util.*;
2. import java.lang.reflect.*;
3.
4. public class ReflectionTest
5. {
6. public static void main(String[] args)
7. {
8. // read class name from command-line args or user input
9. String name;
10. if (args.length > 0)
11. name = args[0];
12. else
13. {
14. Scanner in = new Scanner(System.in);
15. System.out.println("Enter class name (e.g. java.util.Date): ");
16. name = in.next();
17. }
18.
19. try
20. {
21. // print class name and superclass name (if != Object)
22. Class cl = Class.forName(name);
23. Class supercl = cl.getSuperclass();
24. System.out.print("class " + name);
25. if (supercl != null && supercl != Object.class)
26. System.out.print(" extends " + supercl.getName());
27.
28. System.out.print("
{
");
29. printConstructors(cl);
30. System.out.println();
31. printMethods(cl);
32. System.out.println();
33. printFields(cl);
34. System.out.println("}");
35. }
36. catch(ClassNotFoundException e) { e.printStackTrace(); }
37. System.exit(0);
38. }
39.
40. /**
41. Prints all constructors of a class
42. @param cl a class
43. */
44. public static void printConstructors(Class cl)
45. {
46. Constructor[] constructors = cl.getDeclaredConstructors();
47.
48. for (Constructor c : constructors)
49. {
50. String name = c.getName();
51. System.out.print(" " + Modifier.toString(c.getModifiers()));
52. System.out.print(" " + name + "(");
53.
54. // print parameter types
55. Class[] paramTypes = c.getParameterTypes();
56. for (int j = 0; j < paramTypes.length; j++)
57. {
58. if (j > 0) System.out.print(", ");
59. System.out.print(paramTypes[j].getName());
60. }
61. System.out.println(");");
62. }
63. }
64.
65. /**
66. Prints all methods of a class
67. @param cl a class
68. */
69. public static void printMethods(Class cl)
70. {
71. Method[] methods = cl.getDeclaredMethods();
72.
73. for (Method m : methods)
74. {
75. Class retType = m.getReturnType();
76. String name = m.getName();
77.
78. // print modifiers, return type and method name
79. System.out.print(" " + Modifier.toString(m.getModifiers()));
80. System.out.print(" " + retType.getName() + " " + name + "(");
81.
82. // print parameter types
83. Class[] paramTypes = m.getParameterTypes();
84. for (int j = 0; j < paramTypes.length; j++)
85. {
86. if (j > 0) System.out.print(", ");
87. System.out.print(paramTypes[j].getName());
88. }
89. System.out.println(");");
90. }
91. }
92.
93. /**
94. Prints all fields of a class
95. @param cl a class
96. */
97. public static void printFields(Class cl)
98. {
99. Field[] fields = cl.getDeclaredFields();
100.
101. for (Field f : fields)
102. {
103. Class type = f.getType();
104. String name = f.getName();
105. System.out.print(" " + Modifier.toString(f.getModifiers()));
106. System.out.println(" " + type.getName() + " " + name + ";");
107. }
108. }
109. }
---------------------------------------
Using Reflection to Analyze Objects at Run Time
Example 5-6. ObjectAnalyzerTest.java
1. import java.lang.reflect.*;
2. import java.util.*;
3. import java.text.*;
4.
5. public class ObjectAnalyzerTest
6. {
7. public static void main(String[] args)
8. {
9. ArrayList
10. for (int i = 1; i <= 5; i++) squares.add(i * i);
11. System.out.println(new ObjectAnalyzer().toString(squares));
12. }
13. }
14.
15. class ObjectAnalyzer
16. {
17. /**
18. Converts an object to a string representation that lists
19. all fields.
20. @param obj an object
21. @return a string with the object's class name and all
22. field names and values
23. */
24. public String toString(Object obj)
25. {
26. if (obj == null) return "null";
27. if (visited.contains(obj)) return "...";
28. visited.add(obj);
29. Class cl = obj.getClass();
30. if (cl == String.class) return (String) obj;
31. if (cl.isArray())
32. {
33. String r = cl.getComponentType() + "[]{";
34. for (int i = 0; i < Array.getLength(obj); i++)
35. {
36. if (i > 0) r += ",";
37. Object val = Array.get(obj, i);
38. if (cl.getComponentType().isPrimitive()) r += val;
39. else r += toString(val);
40. }
41. return r + "}";
42. }
43.
44. String r = cl.getName();
45. // inspect the fields of this class and all superclasses
46. do
47. {
48. r += "[";
49. Field[] fields = cl.getDeclaredFields();
50. AccessibleObject.setAccessible(fields, true);
51. // get the names and values of all fields
52. for (Field f : fields)
53. {
54. if (!Modifier.isStatic(f.getModifiers()))
55. {
56. if (!r.endsWith("[")) r += ",";
57. r += f.getName() + "=";
58. try
59. {
60. Class t = f.getType();
61. Object val = f.get(obj);
62. if (t.isPrimitive()) r += val;
63. else r += toString(val);
64. }
65. catch (Exception e) { e.printStackTrace(); }
66. }
67. }
68. r += "]";
69. cl = cl.getSuperclass();
70. }
71. while (cl != null);
72.
73. return r;
74. }
75.
76. private ArrayList