Java Reflection Exercises

Source: Internet
Author: User
Tags instance method reflection

In order to recall, and then re-found a few exercises to do the next
Package Cn.incast.homework30;import Java.io.filenotfoundexception;import Java.io.filereader;import Java.io.ioexception;import Java.lang.reflect.constructor;import Java.lang.reflect.field;import Java.lang.reflect.method;import Java.util.arraylist;import Java.util.properties;import Java.util.Scanner;public        Class Reflect {public static void main (string[] args) throws Exception {//Fanxingdemo ();        NewObject1 ();        NewObject2 ();        Printstr ();        Showobj ();        SetProperty (New Person (), "name", "Zhang San");        Beandemo ();            Run ();//System.out.println (GetProperty (New Person (), ' age ')); }//8. Write a method that can get the value of the property named PropertyName in the Obj object public static object GetProperty (Object obj, String PropertyName) throws Excepti            On {Class C =obj.getclass ();            Field F =c.getdeclaredfield (PropertyName);            F.setaccessible (TRUE); Object fvalue = f.get (obj);//System.out.println (Fvalue);           return fvalue; }//7.    (1) Write a configuration file in the properties format, and configure the full name of the class.    (2) Write a program, read the properties configuration file, get the full name of the class and load the class,//(3) Run the Run method in a reflective manner.        private static void Run () throws Exception {Properties Pro = new Properties ();        Note: If you are using eclise, remember to right-click the project name and create a new file, because it cannot be found if the file is not in the project root directory.        FileReader FR = new FileReader ("Config.properties");        Pro.load (FR);        Fr.close ();        String className = Pro.getproperty ("ClassName");        String MethodName = Pro.getproperty ("MethodName");        Class C = class.forname (ClassName);        Object obj = c.newinstance ();        method = C.getmethod (MethodName);    Method.invoke (obj);    }//6. Define a standard JavaBean, named person, that contains the attribute name, age.    Create an instance using reflection, call the constructor to initialize name, age, use reflection to invoke the SetName method to set the name,//Do not use the Setage method to assign an age directly using reflection.        private static void Beandemo () throws Exception {Class c = class.forname ("Cn.incast.homework30.Person"); Constructor con = c.getconstructor (string.claSS, Int.class);        Object obj = con.newinstance ("xiaoming", 16); C.getmethod ("SetName", String.class). Invoke (obj, "Xiao Li");//The Reflection method calls the SetName method to set the name Field f = C.getdeclaredfield ("Name        ");        F.setaccessible (TRUE);    System.out.println (obj); }//5.    Write a method that sets the value of the property named PropertyName in the Obj object to values.  public static void SetProperty (Object obj, String PropertyName, Object value) throws Exception {Class C        = Obj.getclass (); Field f = C.getdeclaredfield (propertyname);//Get the property including private and public f.setaccessible (TRUE);//To cooperate with this, cancel the permission check to set the property F.s        ET (obj, value);    System.out.println (obj); }//4. Write a Class A, add an instance method, Showstring, to print a string, write a class Testa//, as a client, enter a string with the keyboard, which is the full name of Class A,//use the reflection mechanism to create an object of the class, and invoke the object        Method showstring private static void Showobj () throws Exception {Scanner s = new Scanner (system.in);        SYSTEM.OUT.PRINTLN ("Output a full name of a class");        Class C = class.forname (S.nextline ());      Object obj = c.newinstance ();  method = C.getmethod ("showstring");    Method.invoke (obj); }//3. Write a class that adds an instance method for printing a string.    and use reflection to create an object of that class and invoke the method in that object.        private static void Printstr () throws Exception {Class c = class.forname ("Cn.incast.homework30.Person");        Person p = new person ();        Object p = c.newinstance ();        method = C.getmethod ("print");    Method.invoke (P); }//2. Using reflection to create an object, there are 2 ways to use code to reflect the private static void NewObject1 () throws Exception {Class c = class.forname ("Cn.incast . homework30.        Person ");        Constructor cons = C.getconstructor (String.class, Int.class);        Object obj = cons.newinstance ("Xiaoming", 20);    System.out.println (obj);        } private static void NewObject2 () throws Exception {Class c = class.forname ("Cn.incast.homework30.Person");        Object obj = c.newinstance ();    System.out.println (obj); }//1. Generic type erase private static void Fanxingdemo () throws Exception {arraylist<string>Array = new arraylist<string> ();        Array.add ("Begin");        Class C = class.forname ("Java.util.ArrayList");        method = C.getmethod ("Add", Object.class);        Method.invoke (array, 20);        for (Object Obj:array) {System.out.println (obj); }    }}

Java Reflection exercises

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.