java通过反射取得一个类的完整结构

首先我们在person包中新建一个Person.java:

package person;

import sex.Sex;

public class Person{
	private String name = null;
	private int age = 0;
	private Sex sex = null;
	private String birthday = null;
	
	public Person(){};
	public Person(String name, int age, Sex sex, String birthday){
		String birMatch = "\\d{4}-\\d{2}-\\d{2}";
		this.name = name;
		this.age = age;
		this.sex = sex;
		if(birthday.matches(birMatch)){
			this.birthday = birthday;
		}
	}
	public void setInfo(String name, int age, Sex sex, String birthday){
		String birMatch = "\\d{4}-\\d{2}-\\d{2}";
		this.name = name;
		this.age = age;
		this.sex = sex;
		if(birthday.matches(birMatch)){
			this.birthday = birthday;
		}
	}
	public String getPerName(){
		return this.name;
	}
	public int getAge(){
		return this.age;
	}
	public Sex getSex(){
		return this.sex;
	}
	public String getBirthday(){
		return this.birthday;
	}
	public String toString(){
		return this.name + ", " + this.age + " years old, " + this.sex + ", " + this.birthday;
	}
}

  然后在sex包中建立一个枚举Sex.java:

package sex;

public enum Sex{
	MALE, FEMALE;
}

  再在main包的主方法取得Person类的完整结构:

package main;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import person.Person;
import sex.Sex;

public class Main {
	public static void main(String[] args){	
		try {
			Class<?> cls = Class.forName("person.Person");  //打开类
			Field[] var = cls.getDeclaredFields();   //取得类的属性
			Constructor<?>[] consMeth = cls.getConstructors();   //取得类的所有构造函数
			Method[] meth = cls.getMethods();       //取得类的所有方法
			for(int i=0; i<var.length; i++){        //输出属性
				System.out.println(var[i].toGenericString());   
			}
			for(int i=0; i<consMeth.length; i++){     //输出构造函数
				System.out.println(consMeth[i].toGenericString());
			}
			for(int i=0; i<meth.length; i++){          //输出方法
				System.out.println(meth[i].toGenericString());
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

  运行结果:

private java.lang.String person.Person.name
private int person.Person.age
private sex.Sex person.Person.sex
private java.lang.String person.Person.birthday
public person.Person()
public person.Person(java.lang.String,int,sex.Sex,java.lang.String)
public java.lang.String person.Person.toString()
public sex.Sex person.Person.getSex()
public java.lang.String person.Person.getPerName()
public void person.Person.setInfo(java.lang.String,int,sex.Sex,java.lang.String)
public int person.Person.getAge()
public java.lang.String person.Person.getBirthday()
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public boolean java.lang.Object.equals(java.lang.Object)
public native int java.lang.Object.hashCode()
public final native java.lang.Class<?> java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()

  结果说明该类继承得到的方法也可以得到。

转载于:https://www.cnblogs.com/xiaobiqiang/p/6046839.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值