Dark Horse programmer Series 3 reflection, dark horse 3

Source: Internet
Author: User

Dark Horse programmer Series 3 reflection, dark horse 3

ASP. Net + Android + IOS development and Net training. We look forward to communicating with you!

 

(This article refer to the reflection video tutorial of Zhang Xiaoxiang and the blog http://www.cnblogs.com/gulvzhe/archive/2012/01/27/2330001.html of netizens)

 

Directory: 1. Overview 2. Class Introduction 3. How to Use reflection and simple code demonstration

 

I. Overview

The Java reflection mechanism is in the running state. For any class, all attributes and methods of this class can be known. For any object, all of them can call any of its methods and attributes. The information obtained dynamically and the function of calling methods of objects dynamically is called the reflection mechanism of Java language.

Reflection is mainly used in the Framework. The framework needs to call a large number of classes written by framework users. These classes are completed after framework development. Therefore, the framework cannot directly obtain their instance objects through new, instead, use reflection.

The disadvantage of reflection is resource consumption.

 

Ii. Class Introduction

Java has nine predefined Class types (eight basic types and one void type)

You can use the isPrimitive () method in the Class to determine whether the object is a predefined object.

Three methods for getting Class objects

1. Class. forName ("Class Name") returns the Class bytecode, which is directly obtained after being loaded.

2. Object. getClass ()

3. class Name. class

Construct class

Member variables correspond to Filed classes

Method class for member Methods

 

3. How to Apply reflection

To use the java reflection mechanism, follow these three steps:

Sample Code: reflect the instance properties of the operation class, or even forcibly obtain the private properties of the class. There are three methods to obtain the code demonstration of the Class Object.

7 public class ReflectTest {8 9/** objective: to reflect the instance attributes of the operation class, you can even forcibly obtain the private attribute 10 * @ param args11 * @ throws ClassNotFoundException 12 * @ throws IllegalAccessException 13 * @ throws IllegalArgumentException 14 */15 public static void main (String [] args) throws ClassNotFoundException, IllegalArgumentException, IllegalAccessException {16 17 Class cat = Cat. class; // class name. class method to obtain Class Object 18 Class catfor = class. forName ("edu. fjnu. reflect. cat "); // Class. forName ("Class Name") method to obtain Class objects. For convenience, an exception 19 20 Cat cats = new Cat (); 21 Class catget = cats. getClass (); // object. getClass () method to obtain the Class Object 22 23 Field [] catFields = cat. getDeclaredFields (); // obtain all attributes, including private attributes 24 25 for (Field catField: catFields) {26 27 catField. setAccessible (true); // set the attribute value to be accessible, including the private attribute value 28 29 String name = catField. getName (); // get the attribute name 30 Object value = catField. get (cats); // The method parameter is the Instance Object of the class to which the property belongs. if an exception is thrown, 31 32 System is processed. out. println (name + ":" + value); 33} 34 35} 36 37} 38 class Cat {39 private String name = "kitty "; 40 private String color = "white"; 41 private int age = 2; 42}View Code

 

 

Errors are inevitable for beginners. You are welcome to judge and continue to correct ing ...........

 

ASP. Net + Android + IOS development and Net training. We look forward to communicating with you!

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.