集册 Java实例教程 查找带参数的构造函数

查找带参数的构造函数

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

463
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
查找带参数的构造函数


//package com.nowjava;/**来自 时 代 J a v a - N o w J a v a . c o m**/

import java.lang.reflect.Constructor;


public class Main {

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

        Class clazz = String.class;

        int paramCount = 2;

        System.out.println(findConstructorWithParams(clazz, paramCount));

    }


    private static Constructor findConstructorWithParams(Class clazz,

            int paramCount) throws NoSuchMethodException {

        Constructor[] inits = clazz.getConstructors();

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

            if (inits[i].getParameterTypes().length == paramCount) {

                return inits[i];

            }/* from 时代Java*/

        }

        throw 
展开阅读全文