集册 Java实例教程 打印构造函数

打印构造函数

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

508
打印构造函数


//package com.nowjava;


import java.lang.reflect.*;
/** from 
n o w    j a v a  . c o m**/

import static java.lang.System.out;


public class Main {

    public static void printConstructor(Class<?> c) {

        out.format("Constructors => %n");

        Constructor[] cons = c.getDeclaredConstructors();


        if (cons.length == 0) {

            out.format("  --%s", "No declared constructor found");

        } else {

            for (Constructor con : cons) {

                Parameter[] params = con.getParameters();


                StringBuffer sb = new StringBuffer();

                if (params.length != 0) {

                    int i = 0;/** 来自 时代Java - N o w  J a v a . c o m**/

                    for (; i < params.length - 1; i++) {

                        sb.append(params[i].getType().getCanonicalName()

                                + " " + params[i].getName());

                        sb.append(", ");

                    }

                    sb.append(params[i].getType().getCanonicalName() + " "

                            + params[i].getName())
展开阅读全文