java中反射:加载类的构造方法
Posted 虎虎地程序员
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java中反射:加载类的构造方法相关的知识,希望对你有一定的参考价值。
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中反射:加载类的构造方法的主要内容,如果未能解决你的问题,请参考以下文章