1.package com.gufengxiachen.java.reflectiontest;java
public class Person {ide
private String name;函數
private int age;ui
private static int total;this
public Person(){spa
super();orm
total++;接口
public Person(String name,int age){get
super();it
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 {http://www.huiyi8.com/gundongdaima/滾動代碼
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<pts.length;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<pts.length;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