A detailed explanation of Java reflection Mechanism and example code _java

Source: Internet
Author: User
Tags modifier modifiers reflection

Java Reflection Mechanism:

Testing entity Classes

Taking human as an example

/** * PROJECT:DAY12_FOR_LXY * created:lulu * DATE:2016/8/10/public class Human<t> {private String name
  ;
  private int age;

  private float height;
  private static int legs = 2;
  Private map<string, string> jobs;

  Private T T;
    public int Say () {System.out.println ("I ' m" + name);
  return 0;
  } private void Sleep (Human Human) {System.out.println (name + ' sleep with ' + human.name);
  Public list<integer> GetList () {return new arraylist<> ();
    Public Human () {} private Human (String name, int age, float height) {this.name = name;
    This.age = age;
  This.height = height;
  Public String GetName () {return name;
  public void SetName (String name) {this.name = name;
  public int getage () {return age;  public void Setage (int age) {if (Age >/Age < 0) {throw new RuntimeException (' Age > 150 | |
    Age < 0 ");
  } This.age = age; } Public Float GEtheight () {return height;
  public void SetHeight (float height) {this.height = height; 
        @Override public String toString () {return "human{" + "Name= '" + name + ' \ ' + ", age=" + Age +
  ", height=" + Height + '} ';

 }
}

Test Get construct method

/** * PROJECT:DAY12_FOR_LXY * Created:lulu * DATE:2016/8/10////////* Get Construction Method/public class TestConstructor {Publ
    IC static void Main (string[] args) {//get the corresponding class structure, which is used to describe Human class class 

test get field

/** * PROJECT:DAY12_FOR_LXY * Created:lulu * DATE:2016/8/10/* Get Properties: properties include: (  Property name type modifier generic) The property of the parent class/public class Testfield {public static void main (string[] args) {class 

test Fetch method

/** * PROJECT:DAY12_FOR_LXY * Created:lulu * DATE:2016/8/10///* Fetch method: Method name modifier Returns a call to a generic static and Non-static method of a value type * * Publi
    C class TestMethod {public static void main (string[] args) {class 

Test Get class Information

public static void Main (string[] args) {
  printclassinfo (student.class);
}

The class of the only parent class in the public static void Printclassinfo (Class C) {
    //java object
    System.out.println (C.getname ());
    System.out.println (C.getsimplename ());
    System.out.println (C.getsuperclass ());

    Class[] interfaces = C.getinterfaces ();
    for (Class aninterface:interfaces) {
      System.out.println (aninterface);
    }
    The outer class class has only two types of access permission modifiers (public and default)
    int modifiers = C.getmodifiers ();
  }

Thank you for reading, I hope to help you, thank you for your support for this site!

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.