Reflection in Java: How to construct a load class

Source: Internet
Author: User

Reflection in Java: How to construct a load class

Package Com.ma.reflection;import Java.lang.reflect.constructor;import Java.lang.reflect.InvocationTargetException Import Org.junit.test;import com.ma.bean.userbean;/** * Reflection: Load Construction Method * @author MA * */public class Demo2 {/** * Reflection: The parameterless constructor of the load class Method */@Testpublic void test1 () {//1. Called with the Class.forName ("Class name") method; try {//Reflection class Class<?> Clazz = Class.forName (" Com.ma.bean.UserBean ");//Gets the constructor of the class 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 ();}} /** * Reflection: The method of constructing the loaded class with a parameter * @throws classnotfoundexception * @throws invocationtargetexception * @throws IllegaLaccessexception * @throws instantiationexception * @throws illegalargumentexception * @throws nosuchmethodexception * @throws SecurityException */@Testpublic void Test2 () throws ClassNotFoundException, IllegalArgumentException, Instantiationexception, Illegalaccessexception, InvocationTargetException, SecurityException, Nosuchmethodexception {//Reflection class Class<?> Clazz = Class.forName ("Com.ma.bean.UserBean"); constructor<?> C = clazz.getconstructor (String.class,string.class,string.class); UserBean user = (UserBean) c.newinstance ("001", "John Doe", "123abc"); SYSTEM.OUT.PRINTLN (user);} /** * Reflection: The private and constructive method of loading class * @throws classnotfoundexception * @throws nosuchmethodexception * @throws SecurityException * @t Hrows invocationtargetexception * @throws illegalaccessexception * @throws instantiationexception * @throws Illegalargu Mentexception */@Testpublic 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 ("Harry"); SYSTEM.OUT.PRINTLN (user);} /** * * Another method of non-parametric reflection * @throws classnotfoundexception * @throws illegalaccessexception * @throws instantiationexception * /@Testpublic void Test4 () throws ClassNotFoundException, Instantiationexception, illegalaccessexception {class<? > clazz = Class.forName ("Com.ma.bean.UserBean"); UserBean user = (UserBean) clazz.newinstance (); SYSTEM.OUT.PRINTLN (user);}}

Entity class

Package com.ma.bean;/** * UserBean entity class * @author MA * */public class UserBean {private string Id;private string name = "Zhang San" ;p rivate String password;public UserBean () {super (); System.out.println ("I am a non-parametric construction method");} Public UserBean (string ID, string name, string password) {super (); this.id = Id;this.name = Name;this.password = Password;s Ystem.out.println ("I am a constructive method");} 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;} @Overridepublic String toString () {return "UserBean [id=" + ID + ", name=" + name + ", password=" + password + "]";} Private UserBean (String name) {this.name = name; SYSTEM.OUT.PRINTLN (name);} }

  

Reflection in Java: How to construct a load class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.