获得类的各种信息方法(反射)
类
package Reflection;
public class Demo01 {
public static void main ( String[ ] args) throws ClassNotFoundException {
Person person = new Student ( ) ;
System. out. println ( person. name) ;
Class c1 = person. getClass ( ) ;
System. out. println ( c1. hashCode ( ) ) ;
Class c2 = Class. forName ( "Reflection.Student" ) ;
System. out. println ( c2. hashCode ( ) ) ;
Class c3 = Student. class ;
System. out. println ( c3. hashCode ( ) ) ;
Class c4 = Integer. class ;
System. out. println ( c4. hashCode ( ) ) ;
Class c5 = c1. getSuperclass ( ) ;
System. out. println ( c5. hashCode ( ) ) ;
System. out. println ( "==================================分割线==================================" ) ;
System. out. println ( "只要元素类型与维度一样,就是同一个class" ) ;
int [ ] a1 = new int [ 10 ] ;
int [ ] a2 = new int [ 100 ] ;
int [ ] a3 = new int [ 1000 ] ;
int [ ] [ ] b1 = new int [ 10 ] [ 10 ] ;
int [ ] [ ] b2 = new int [ 100 ] [ 100 ] ;
int [ ] [ ] b3 = new int [ 1000 ] [ 1000 ] ;
System. out. println ( a1. getClass ( ) . hashCode ( ) ) ;
System. out. println ( a2. getClass ( ) . hashCode ( ) ) ;
System. out. println ( a3. getClass ( ) . hashCode ( ) ) ;
System. out. println ( b1. getClass ( ) . hashCode ( ) ) ;
System. out. println ( b2. getClass ( ) . hashCode ( ) ) ;
System. out. println ( b3. getClass ( ) . hashCode ( ) ) ;
}
}
class Person {
public String name;
public Person ( ) {
}
public Person ( String name) {
this . name = name;
}
@Override
public String toString ( ) {
return "Person{" +
"name='" + name + '\'' +
'}' ;
}
}
class Student extends Person {
String sex;
public Student ( ) {
this . name = "学生" ;
}
public Student ( String name, int a) {
this . name = "学生" ;
}
public void say ( String a) {
System. out. println ( a) ;
}
}
class Teacher extends Person {
public Teacher ( ) {
this . name = "老师" ;
}
}
获取信息类
package Reflection;
import com. sun. xml. internal. ws. api. model. MEP;
import java. lang. reflect. Constructor;
import java. lang. reflect. Field;
import java. lang. reflect. Method;
public class Demo03 {
public static void main ( String[ ] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
Class c1 = Class. forName ( "Reflection.Student" ) ;
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[ ] fields2 = c1. getDeclaredFields ( ) ;
for ( Field field: fields2) {
System. out. println ( field) ;
}
Field name = c1. getDeclaredField ( "sex" ) ;
System. out. println ( name) ;
System. out. println ( "===================分割线===================分割线" ) ;
Method[ ] methods = c1. getMethods ( ) ;
for ( Method method: methods) {
System. out. println ( method) ;
}
Method[ ] methods2 = c1. getDeclaredMethods ( ) ;
for ( Method method: methods2) {
System. out. println ( method) ;
}
Method method = c1. getMethod ( "say" , String. class ) ;
System. out. println ( method) ;
System. out. println ( "===================分割线===================分割线" ) ;
Constructor[ ] constructors = c1. getConstructors ( ) ;
for ( Constructor c: constructors) {
System. out. println ( c) ;
}
Constructor constructor = c1. getDeclaredConstructor ( String. class , int . class ) ;
System. out. println ( constructor) ;
}
}