Reflection operations classes, methods, properties, etc.

Source: Internet
Author: User

1. Operation class

Gets the class and gets an instance object through reflection

Class Class1 = Student.class;

Student Student = (Student) class1.newinstance (); Default call no parameter construction method

Student.setname ("heh");

System.out.println (Student.getname ());



2. Operation Construction Method

gets the constructor method for the specified parameter type that creates an instance object of a specific parameter value from this object

Class Class1 = Student.class;

constructor<?> Constructor = Class1.getconstructor (String.class,string.class,int.class);

Student student1= (Student) constructor.newinstance ("123", "456", 12);

System.out.println (Student1.getage ());


3. General method of operation

Class Class1 = Student.class;

Student student= (Student) class1.newinstance ();

Method Method=class1.getmethod ("Setage", Int.class);

Method.invoke (student,10);

System.out.println (Student.getage ());

4. Operation Properties

Class Class1 = Student.class;

Student Student = (Student) class1.newinstance ();

Field Fiel=class1.getdeclaredfield ("Age");

Fiel.setaccessible (TRUE); The member variable in the Accessibletest class is private, so you must do this

Fiel.set (student, 10);

System.out.println (Student.getage ());


5. Modifying the value of an array

Int[] a= {1,2,3,4,5,6,7,8,9};

Class Class1 = A.getclass (). Getcomponenttype (); Returns the Class that represents the array component type. If this class does not represent an array class, this method returns null

System.out.println ("Name of the array data:" +class1.getname ());

System.out.println ("Length of the array:" +array.getlength (a));

System.out.println ("The value of the first element of the data:" +array.get (a,0));

Array.set (A, 0, 12);

System.out.println ("The value of the first element of the data:" +array.get (a,0));




This article is from the "Programmer rookie" blog, be sure to keep this source http://5345468.blog.51cto.com/5335468/1690318

Reflection operations classes, methods, properties, etc.

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.