集册 Java实例教程 获取类声明的字段

获取类声明的字段

欢马劈雪     最近更新时间:2020-01-02 10:19:05

467
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
获取类声明的字段
/** 
来 自 
nowjava.com - 时  代  Java
**/


//package com.nowjava;

import java.lang.reflect.Constructor;

import java.lang.reflect.Field;

import java.lang.reflect.Method;

import java.lang.reflect.Modifier;


public class Main {

    public static void main(String[] argv) throws Exception {

        Class classInstance = String.class;

        getClassDeclaredFields(classInstance);

    }


    public static void getClassDeclaredFields(Class<?> classInstance) {


        Field[] ff = classInstance.getDeclaredFields();

        String x;

        /*
        来 自*
         n o w j a v a . c o m - 时  代  Java
        */

        for (int i = 0; i < ff.length; i++) {

            x = ff[i].getType().getName();

            System.out.println("???????:" + x);

        }


        System.out.println("----------------------------=");


        Constructor<?>[] cn = classInstance.getDeclaredConstructors();

        for (int i = 0; i < cn.length; i++) {

            Class cx[] = cn[i].getParameterTypes();

            System.out.println("???? :" + cn[i].getName());

            for (int j = 0; j < cx.length; j++) {

                x = cx[j].getName();

                System.out.println("---------???? ?????? :" + x);

            }

        }


        System.out.println("----------------------------=");

        Method[] mm = classInstance.getDeclaredMethods();


        for (int i = 0; i < mm.length; i++) {

            System.out.println("????? :" + mm[i].getName());


            int md = mm[i].getModifiers();

            System.out.print("??? :" + Modifier.toString(md));


            Class cx[] = mm[i].getParameterTypes();

            
展开阅读全文