Reflection concept: Three ways to get class file objects

Source: Internet
Author: User

Reflection

The Java reflection mechanism is in the running state, for any class, can know all the properties and methods of this class, for any one object, can call any of its methods and properties; This dynamically acquired information and the ability to dynamically invoke the object's methods are called the reflection mechanisms of the Java language.

To dissect a class, you must first obtain the bytecode file object for that class. The anatomy uses the method in class. So first get the object of class type corresponding to each bytecode file.

Using the class file object, use the member variables in the file, construct methods, and member methods.

Example 1: Three ways to get class file objects

Public class person {

Private Stringname;

int age;

Public Stringaddress;

Public Person () {

}

Private Person (String name) {

this. name = name;

}

Person (Stringname, int.) {

this. name = name;

this. age = age;

}

Public Person (string name,int age, string address) {

this. name = name;

this. age = age;

this. address = address;

}

Public void Show () {

System. out. println ("show");

}

Public void method (String s) {

System. out. println ("method"+ s);

}

Public String getString (Strings,inti) {

return s +"---" + i;

}

private void function () {

System. out. println ("function");

}

@Override

Public String toString () {

return "person [name=]+ name+ ", age= "+ age + ", address= " + Address

+"]";

}

}

/*

 * Reflection: It is through class a file object that goes to use the member variable in the file, constructs the method, and the member method.

*

* Person P = new person ();

* p. Use

*

 * To use it like this, first you have to get class the file object is actually getting Class the object of the class.

* Class class:

 *    member Variables Field

 *    Construction Method Constructor

 *    member Methods Method

*

 * Get class file objects in the way:

* A:object Class of getclass () Method

* B: static properties of the data type class

* C:class static methods in a class

* publicstatic Class forname (String className)

*

 * who do we actually use? ?

* A: Play by Yourself Choose one, the second is more convenient

* B: Development Third Kind

 *        for what? ? because the third type is a string, not a specific class name. This allows us to configure such a string in the configuration file.

*/

Public class Reflectdemo {

Public static void main (string[] args)throws classnotfoundexception{

// Way 1

PERSONP = newperson ();

Classc = P.getclass ();

PERSONP2 = newperson ();

Classc2 = P2.getclass ();

System. out. println (p = = p2); //False

System. out. println (c = = C2); //True

// Way 2

Classc3 = Person . class;

//Int.class;

//String.class;

System. out. println (c = = C3);

// Way 3

//ClassNotFoundException

classC4 = class. Forname("day27. Person ");

System. out. println (c = = C4);

}

}

Operation Result:

False

True

True

True

Reflection concept: Three ways to get class file objects

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.