1.package com.gufengxiachen.java.reflectiontest;
public class Person {
private String name;
private int age;
private static int total;
public Person(){
super();
total++;
public Person(String name,int age){
super();
this.name=name;
this.age=age;
total++;
public void setName(String name) {
this.name = name;
public String getName() {
return name;
public void setAge(int age) {
this.age = age;
public int getAge() {
return age;
public static int getTotal() {
return total;
public static void setTotal(int total) {
Person.total = total;
@Override
public String toString() {
// TODO 自动生成方法存根
return "Name:"+name+"\tAge:"+age;
2.package com.gufengxiachen.java.reflectiontest;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Scanner;
public class TestReflection01 {
public static void main(String[] args) {
System.out.println("请输入类型名称:");
Scanner scan=new Scanner(System.in);
String className=scan.next();
System.out.println("当前类型:"+className);
try {
Class c=Class.forName(className);
Field[] fs=c.getDeclaredFields();
for(Field f:fs){
System.out.println("--------------------------");
System.out.println("属性:"+f.toString());
System.out.println("\t数据类型:"+f.getType());
System.out.println("\t属性名:"+f.getName());
int mod=f.getModifiers();
System.out.println("\t属性修饰符:"+Modifier.toString(mod)+Modifier.isPrivate(mod));
Method[] ms=c.getDeclaredMethods();
for(Method m:ms){
System.out.println("...........................................");
System.out.println("方法:"+m.toString());
System.out.println("\t方法名:"+m.getName());
int mod=m.getModifiers();
System.out.println("\t方法修饰符:"+Modifier.toString(mod));
System.out.println("\t方法参数列表:");
Class pts[]=m.getParameterTypes();
for(int i=0;i
Class class1=pts[i];
if(i!=0)
System.out.println(",");
System.out.println(class1);
System.out.println();
System.out.println("\t返回值类型:"+m.getReturnType());
Constructor[] cs=c.getDeclaredConstructors();
for(Constructor con:cs){
System.out.println("******************************************");
System.out.println("构造方法:"+con.toString());
System.out.println("方法名:"+con.getName());
int mod=con.getModifiers();
System.out.println("\t方法修饰符:"+Modifier.toString(mod));
System.out.print("\t方法参数列表:");
Class pts[]=con.getParameterTypes();
for(int i=0;i
Class class1=pts[i];
if(i!=0)
System.out.print(",");
System.out.println(class1);
System.out.println();
System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
Class superClass=c.getSuperclass();
System.out.println("当前类的父类:"+superClass.toString());
System.out.println("##########################################");
Class[] interfaces=c.getInterfaces();
System.out.print("当前类所实现接口:");
for(Class class1:interfaces){
System.out.println(class1.toString()+"\t");
System.out.println("\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
Package p=c.getPackage();
System.out.println("当前类所在包:"+p.toString());
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
3.package com.gufengxiachen.java.reflectiontest;
public class User {
public String name;
public int age;
public static int total=100;
public User() {
// TODO 自动生成构造函数存根
super();
public User(String name,int age){
super();
this.name=name;
this.age=age;
public void setAll(String name,int age){
this.name=name;
this.age=age;
public static void showTotal(){
System.out.println("int static method showTotal,total= "+total);
@Override
public String toString() {
// TODO 自动生成方法存根
return "用户名:"+name+"\t密码:"+age