JDK工具类_____反射的基本应用

package test;

import java.io.Serializable;

/**
 * @author Administrator
 *
 */
public class User implements Serializable{
	/**
	 * SERID
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * genral po
	 */
	private String username;
	private String password;
	/**
	 * encapsulation
	 */
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	/**
	 * over tostring
	 */
	@Override
	public String toString() {
		return "User [username=" + username + ", password=" + password + "]";
	}
	
}


package test;


/**
 * @author Administrator
 * Java反射
 */
public class Reflect {
	public static void main(String[] args) {
		User u=new User();
		System.out.println("全类名字:"+u.getClass().getName());
		try {
			//标准的类装载 和jdbc驱动一样
			Class<?>clazz=Class.forName("test.User");
			System.out.println("装载获取class的全名称:"+clazz.getName());
			System.out.println("装载获取class构造器:"+clazz.getConstructors());
			System.out.println("装载获取class所有字段:"+clazz.getFields());
			System.out.println("装载获取class所有方法:"+clazz.getMethods());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
	}
}


//运行结果


//

posted @ 2017-02-14 13:58  蜜獾互联网  阅读(1)  评论(0编辑  收藏  举报  来源