for(Method m :method){ System.out.println(m.getDeclaringClass()); System.out.println(m.getName()); } } public void printMessage(){ System.out.println("Created successful!"); } } 运行结果:
[plain] view plaincopy Created successful! Created successful! 14576877 12677476 class ClassTest1 printMessage class ClassTest1 main class 亚博电竞真实吗.lang.Object wait class 亚博电竞真实吗.lang.Object wait class 亚博电竞真实吗.lang.Object wait class 亚博电竞真实吗.lang.Object hashCode class 亚博电竞真实吗.lang.Object getClass class 亚博电竞真实吗.lang.Object equals class 亚博电竞真实吗.lang.Object toString class 亚博电竞真实吗.lang.Object notify class 亚博电竞真实吗.lang.Object notifyAll 2.TestClass.亚博电竞真实吗
[亚博电竞真实吗] view plaincopy /** * */ public class TestClass { public static void main(String[] args) { try { // 测试Class.forName() Class testTypeForName = Class.forName("TestClassType"); System.out.println("testForName---" + testTypeForName); // 测试类名.class Class testTypeClass = TestClassType.class; System.out.println("testTypeClass---" + testTypeClass); // 测试Object.getClass() TestClassType testGetClass = new TestClassType(); System.out.println("testGetClass---" + testGetClass.getClass()); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }