反射类中的构造方法

package com.javase.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;

/**
 * 反射类中的构造方法
 */
public class ReflectConstructorText01 {
    public static void main(String[] args) {
        //创建一个字符串拼接对象
        StringBuilder s = new StringBuilder();
        try {
            //获取类
            Class vipClass = Class.forName("com.javase.bean.Vip");
            //public class Vip {
            s.append(Modifier.toString(vipClass.getModifiers()));//拼接类的修饰符
            s.append(" class ");
            s.append(vipClass.getSimpleName());//拼接类名
            s.append("{\n");//拼接类体开始符
            Constructor[] constructors = vipClass.getConstructors();//获取类中的所有构造方法
            //遍历
            for (Constructor constructor : constructors){
                //public Vip(int no, String name, String birth) {
                s.append("\t");//拼接制表符
                s.append(Modifier.toString(constructor.getModifiers()));//拼接构造方法修饰符
                s.append(" ");//空格
                s.append(vipClass.getSimpleName());//拼接构造方法名(构造方法名与类名一样)
                s.append("(");
                //拼接参数
                //获取参数
                Class[] parameterTypes = constructor.getParameterTypes();
                //遍历
                for (Class parameterType : parameterTypes){
                    s.append(parameterType.getSimpleName());
                    s.append(",");
                }
                if (parameterTypes.length > 0){//当参数类型数组中有元素的时候删除最后一个字符“,”
                    s.deleteCharAt(s.length() - 1);
                }
                s.append("){}\n");
            }

            s.append("}");//拼接类体结束符
            System.out.println(s);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
package com.javase.bean;

public class Vip {
    int no;
    String name;
    String birth;
    boolean sex;

    public Vip() {
    }

    public Vip(int no) {
        this.no = no;
    }

    public Vip(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public Vip(int no, String name, String birth) {
        this.no = no;
        this.name = name;
        this.birth = birth;
    }

    public Vip(int no, String name, String birth, boolean sex) {
        this.no = no;
        this.name = name;
        this.birth = birth;
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Vip{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", birth='" + birth + '\'' +
                ", sex=" + sex +
                '}';
    }
}
/*
程序运行结果:
    public class Vip{
        public Vip(int,String,String,boolean){}
        public Vip(int,String,String){}
        public Vip(int,String){}
        public Vip(int){}
        public Vip(){}
    }
 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值