1. 程式人生 > >java中反射:加載類的構造方法

java中反射:加載類的構造方法

lan iat abc private out exc @override 私有 try

java中反射:加載類的構造方法

package com.ma.reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import org.junit.Test;
import com.ma.bean.UserBean;

/**
 * 反射:加載構造方法
 * @author ma
 *
 */
public class Demo2 {
	
	/**
	 * 反射:加載類的無參構造方法
	 */
	@Test
	public void test1(){
		//1.用Class.forName("類名")方法來調用;
		try {
			//反射類
			Class<?> clazz = Class.forName("com.ma.bean.UserBean");
			
			//得到類的構造函數
			Constructor<?> c = clazz.getConstructor(null);
			
			
			UserBean user = (UserBean) c.newInstance(null);
			System.out.println(user);
			
		
		
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} 
	
	}
	/**
	 * 反射:加載類的有參構造方法
	 * @throws ClassNotFoundException 
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws IllegalArgumentException 
	 * @throws NoSuchMethodException 
	 * @throws SecurityException 
	 */
	@Test
	public void test2() throws ClassNotFoundException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException {
		//反射類
		Class<?> clazz = Class.forName("com.ma.bean.UserBean");
		Constructor<?> c = clazz.getConstructor(String.class,String.class,String.class);
		UserBean user = (UserBean) c.newInstance("001","李四","123abc");
		System.out.println(user);
	}
	
	/**
	 * 反射:加載類的私有有參構造方法
	 * @throws ClassNotFoundException 
	 * @throws NoSuchMethodException 
	 * @throws SecurityException 
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws IllegalArgumentException 
	 */
	@Test
	public void test3() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
		Class<?> clazz = Class.forName("com.ma.bean.UserBean");
		Constructor<?> c = clazz.getDeclaredConstructor(String.class);
		c.setAccessible(true);
		UserBean user = (UserBean) c.newInstance("王五");
		System.out.println(user);
	}
	
	/**
	 * 
	 * 另一種無參反射方法
	 * @throws ClassNotFoundException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	@Test
	public void test4() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
		Class<?> clazz = Class.forName("com.ma.bean.UserBean");
		UserBean user = (UserBean) clazz.newInstance();
		System.out.println(user);
	}
}

  實體類

package com.ma.bean;
/**
 * UserBean實體類
 * @author ma
 *
 */
public class UserBean {
	
	private String id;
	private String name = "張三";
	private String password;
	public UserBean() {
		super();
		System.out.println("我是無參構造方法");
	}
	public UserBean(String id, String name, String password) {
		super();
		this.id = id;
		this.name = name;
		this.password = password;
		System.out.println("我是有參構造方法");
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	@Override
	public String toString() {
		return "UserBean [id=" + id + ", name=" + name + ", password="
				+ password + "]";
	}
	
	private UserBean(String name){
		this.name = name;
		System.out.println(name);
	} 
	

}

  

java中反射:加載類的構造方法