码迷,mamicode.com
首页 > 编程语言 > 详细

java中反射:加载类的构造方法

时间:2018-05-31 12:51:24      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:cto   lan   port   eth   throw   override   tst   ati   word   

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中反射:加载类的构造方法

标签:cto   lan   port   eth   throw   override   tst   ati   word   

原文地址:https://www.cnblogs.com/majingang/p/9116051.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!