1: Instantiating an object through a parameterless construct package cn.itcast;/* * Instantiate an object by using a parameterless construct * Instantiate an object with the class itself, use the Newinstance method * It is important to note that there is a non-parametric construction method in the instantiation class. The person object cannot be instantiated if it does not exist; * If no exception is reported: Instantionexception * */public class ClassDemo03 {public static void main (string[] Ar GS) {//Create class object class Class1 = null; try {class1=class. forname ("Cn.itcast.Person"); } catch (ClassNotFoundException e) {//TODO auto-generated catch block E.printstacktrace (); }//Declares person object person p1 = null; Instantiate the Person object try {p1 = (person) class1.newinstance (); } catch (Instantiationexception e) {//TODO auto-generated catch block E.printstacktrace (); } catch (Illegalaccessexception e) {//TODO auto-generated catch block E.printstacktra CE (); } System. Out.println (p1); P1.setname ("Xiao Li"); P1.setage (20); P1.setsex (' Male '); System. Out.println (p1); }} Print Result: Person [name=null, age=0,sex=]person [name= Xiao Li, age=20, sex= male]
2: Instantiating an object by using a parametric construct if there is really no parameterless construct in the instantiated class, you need to explicitly call the constructor in the class and pass the parameters in and you can instantiate the operation; public final class Constructor<t>extends Accessibleobjectimplements genericdeclaration, member operation step: 1: Gets the parameter construct of the class to instantiate constructor<?> C2 = Class1.getconstructor (String. Class,int.class, Char.class);//Get all the construction methods constructor[] C1 = Class1.getconstructors (); for (Constructor getcon:c1) {//The output is all public method system. OUT.PRINTLN (Getcon);} Printed: Public Cn.itcast.Person () public Cn.itcast.Person (Java.lang.string,int,char) 2: Through C1=newinstance (), Instanced Object Code Presentation: Package Cn.itcast;import Java.lang.reflect.constructor;public class ClassDemo04 {public static void main (St Ring[] (args) {//Create Class object class<?> class1 = null; try {class1=class. forname ("Cn.itcast.Person"); } catch (ClassNotFoundException e) {//TODO auto-generated catch block E.printstacktrace (); }//Declares person object person p1 = null; Get all the construction methods constructor[] C1 = Class1.getconstructors (); for (Constructor getcon:c1) {//The output is all public method system. OUT.PRINTLN (Getcon);//Syste M.out.println (Getcon.getname ());//System.out.println (Getcon.getparametertypes ());//System.out.pri Ntln (Getcon.getmodifiers ()); } try{//Gets the parameter constructs in the person class constructor<?> C2 = Class1.getconstructor (St Ring.class, Int.class, Char.class); P1 = (person) c2. Newinstance ("Xiao Zhang", 24, ' Male '); } catch (Exception e) {e.printstacktrace (); } System. Out.println (p1); }}
Package Cn.itcast;public class Person {private String name;private int age;private char sex;public person () {super ();} Public person (String name, int age, char sex) {super (); this.name = Name;this.age = Age;this.sex = sex;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} Public Char Getsex () {return sex;} public void Setsex (char sex) {this.sex = sex;} public void Eat () {SYSTEM.OUT.PRINTLN ("ate");} @Overridepublic String toString () {return "person [name=" + name + ", age=" + Age + ", sex=" + Sex + "]";}}
Java Reflection mechanism (use of Class)