Java Learning Notes-reflection

Source: Internet
Author: User

Java Learning Notes-reflection

Pending completion

Http://www.cnblogs.com/rollenholt/archive/2011/09/02/2163758.html

Http://www.cnblogs.com/jqyp/archive/2012/03/29/2423112.html

http://coolszy.iteye.com/blog/569846

Http://www.cnblogs.com/octobershiner/archive/2012/03/18/2404751.html

Package Com.evor.test;import Java.lang.reflect.constructor;import java.lang.reflect.InvocationTargetException; public class Test1{public static void Main (String args[]) {class<?> demo1 = null;//class type class<?> demo2=null; Class<?> demo3=null;try{demo1=class.forname ("Com.evor.test.Person");//Get class type}catch (Exception e) {}demo2 = new Person (). GetClass (); Another way to obtain class type DEMO3 = Person.class; Another way to get the class type System.out.println ("Class name" +demo1.getname ()); System.out.println ("Class name" +demo2.getname ()); System.out.println ("Class name" +demo3.getname ()); try {constructor<?> C1 =demo1.getdeclaredconstructor (); SYSTEM.OUT.PRINTLN (C1);//constructor name constructor<?> con = demo2.getdeclaredconstructor (new Class[]{string.class, String.class,string.class,int.class}); System.out.println (con); try {Object obj1 = c1.newinstance (New object[]{});//Create an Instance object Obj2 = Con.newinstance (new object[]{"20000001", "Li Ming", "Male", 19}); Create an instance of System.out.println (OBJ1); System.out.println (OBJ2);d emo1.getmethod ("GetInfo", null);} catch (ExCeption e) {e.printstacktrace ();}} catch (Exception e) {e.printstacktrace ();}}} Class Person{private string sno;p rivate string sname;p rivate string sex;p rivate int age;public person (String Sno, Strin G sname, String sex, int age) {This.sno = Sno;this.sname = Sname;this.sex = Sex;this.age = age;} Public person () {This.sno = "unknow"; this.sname = "Unknow"; this.sex = "Unknow"; this.age = 0;} public void Setsno (String sno) {This.sno = sno;} public void Setsname (String sname) {this.sname = sname;} public void Setsex (String sex) {this.sex = sex;} public void Setage (int.) {this.age = age;} Public String Getsno () {return sno;} Public String Getsname () {return sname;} Public String Getsex () {return sex;} public int getage () {return age;} Public String GetInfo () {return "study number:" +sno+ ", Name:" +sname+ ", Gender:" +sex+ ", Age:" +string.valueof (Ages);}}

  

Java Learning Notes-reflection

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.