Class获取运行时类的完整结构
User类的类
package com. heima. reflection;
public class test_02 extends Object {
public static void main ( String[ ] args) throws ClassNotFoundException {
Class c1 = Class. forName ( "com.heima.reflection.User" ) ;
System. out. println ( c1) ;
Class c2 = Class. forName ( "com.heima.reflection.User" ) ;
Class c3 = Class. forName ( "com.heima.reflection.User" ) ;
Class c4 = Class. forName ( "com.heima.reflection.User" ) ;
System. out. println ( c1. hashCode ( ) ) ;
System. out. println ( c2. hashCode ( ) ) ;
System. out. println ( c3. hashCode ( ) ) ;
System. out. println ( c4. hashCode ( ) ) ;
}
}
class User {
public String nationality;
private String name;
private int id;
private int age;
public User ( ) {
}
public User ( String nationality, String name, int id, int age) {
this . nationality = nationality;
this . name = name;
this . id = id;
this . age = age;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getId ( ) {
return id;
}
public void setId ( int id) {
this . id = id;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
@Override
public String toString ( ) {
return "User{" +
"name='" + name + '\'' +
", id=" + id +
", age=" + age +
'}' ;
}
}
测试类
package com. heima. reflection;
import java. lang. reflect. Constructor;
import java. lang. reflect. Field;
import java. lang. reflect. Method;
public class test_08 {
public static void main ( String[ ] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
Class c1 = Class. forName ( "com.heima.reflection.User" ) ;
User user = new User ( ) ;
c1 = user. getClass ( ) ;
System. out. println ( c1. getName ( ) ) ;
System. out. println ( c1. getSimpleName ( ) ) ;
System. out. println ( "==========================================" ) ;
Field[ ] fields = c1. getFields ( ) ;
for ( Field field : fields) {
System. out. println ( field) ;
}
Field[ ] fields1 = c1. getDeclaredFields ( ) ;
for ( Field field1 : fields1) {
System. out. println ( field1) ;
}
Field name = c1. getDeclaredField ( "name" ) ;
System. out. println ( name) ;
System. out. println ( "====================================" ) ;
Method[ ] methods = c1. getMethods ( ) ;
for ( Method method : methods) {
System. out. println ( "正常的" + method) ;
}
System. out. println ( "----------------------" ) ;
Method[ ] declaredMethods = c1. getDeclaredMethods ( ) ;
for ( Method declaredMethod : declaredMethods) {
System. out. println ( declaredMethod) ;
}
System. out. println ( "---------------" ) ;
Method getName = c1. getMethod ( "getName" , null) ;
Method setName = c1. getMethod ( "setName" , String. class ) ;
System. out. println ( getName) ;
System. out. println ( setName) ;
System. out. println ( "=================================================" ) ;
Constructor[ ] constructors = c1. getConstructors ( ) ;
for ( Constructor constructor : constructors) {
System. out. println ( constructor) ;
}
Constructor[ ] declaredConstructors = c1. getDeclaredConstructors ( ) ;
for ( Constructor declaredConstructor : declaredConstructors) {
System. out. println ( declaredConstructor) ;
}
System. out. println ( "---------------" ) ;
Constructor declaredConstructor = c1. getDeclaredConstructor ( String. class , String. class , int . class , int . class ) ;
System. out. println ( declaredConstructor) ;
}
}