"Automation __gui Automation" __java__ Case __ Class Reflection

Source: Internet
Author: User

One, the code is as follows 1.1, was fired class
 PackageWww.woniu.gui.one; Public classTest {Private intAge= 30; PrivateString name= "Zhang San";  Public intGetage () {returnAge ; }         Public voidSetage (intAge ) {         This. Age =Age ; }    PrivateString GetName () {returnname; }         Public voidsetName (String name) { This. Name =name; }    }

1.2. Class of Operation Reflection
Package Www.woniu.gui.one;import Java.lang.reflect.field;import Java.lang.reflect.method;public class testreflection {public static void main (string[] args) throws Exception {//TODO auto-generated method stubtestreflection Tr= new Testreflection (); Tr.reflection ();} private void Reflection () throws Exception {//TODO auto-generated method stubclass<?> clazz= class.forname ("Www.wo Niu.gui.one.Test "); Object obj= clazz.newinstance (); Method method1= Clazz.getdeclaredmethod ("Setage", Int.class); Method1.invoke (obj, 500); Method method2= Clazz.getdeclaredmethod ("Getage"); int age= (int) method2.invoke (obj); System.out.println (age); Field field1= Clazz.getdeclaredfield ("name"); Field1.setaccessible (true); Field1.set (obj, "John Doe"); System.out.println (Field1.get (obj)); Field field2= Clazz.getdeclaredfield ("Age"); Field2.setaccessible (true); Field2.set (obj, 33); System.out.println (Field2.get (obj)); Method method3= Clazz.getdeclaredmethod ("GetName"); Method3.setaccessible (true); String Name= (String) METHOD3.INVOke (obj); SYSTEM.OUT.PRINTLN (name);}}

"Automation __gui Automation" __java__ Case __ Class 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.