A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 王责彬 中级黑马   /  2014-8-25 03:38  /  1122 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package Test;
import java.lang.*;
import java.lang.reflect.Constructor;

public class GetConstructorsDem {
        String s;
        int a,b,c;
        private GetConstructorsDem(){
        }
        protected GetConstructorsDem(String s,int a){
                this.s=s;
                this.a=a;
        }
        public GetConstructorsDem(String...strings) throws NumberFormatException{
                if(0<strings.length)
                        a=Integer.valueOf(strings[0]);
                if(1<strings.length)
                        b=Integer.valueOf(strings[1]);
                if(2<strings.length)
                        c=Integer.valueOf(strings[2]);
        }
        public void print(){
                System.out.println("s="+s);
                System.out.println("a="+a);
                System.out.println("b="+b);
                System.out.println("c="+c);
               
        }
        public static void main(String[] args) {
                GetConstructorsDem gc=new GetConstructorsDem();
                Class cc=gc.getClass();
                Constructor[] declaredConstructors=cc.getDeclaredConstructors();
                for(int i=0;i<declaredConstructors.length;i++){
                        Constructor constructor=declaredConstructors;
                        System.out.println("查看是否允许带有可变数量的参数:"+declaredConstructors+constructor.isVarArgs());
                        System.out.println("该构造函数的入口参数类型依次为:");
                        Class[] parameterTypes=constructor.getParameterTypes();
                        for(int j=0;j<parameterTypes.length;j++){
                                System.out.println(""+parameterTypes[j]);
                        }
                        System.out.println("该构造函数可能抛出的一场类型为:");
                        Class[] exceptionTypes=constructor.getExceptionTypes();
                        for(int j=0;j<exceptionTypes.length;j++){
                                System.out.println(""+exceptionTypes[j]);
                               
                        }
                        GetConstructorsDem gc1=null;
                        while(gc1==null){
                                try{
                                        if(i==2)
                                                gc1=(GetConstructorsDem)constructor.newInstance();
                                        else if(i==1)
                                                gc1=(GetConstructorsDem)constructor.newInstance("7",5);
                                        else{
                                                Object[] parameters=new Object[]{
                                                                new String[]{"100","200","300"}};
                                                gc1=(GetConstructorsDem)constructor.newInstance(parameters);
                                                }
                                }catch(Exception e){
                                        System.out.println("在创建对象时抛出异常,下面执行setAccessible()方法");
                                        constructor.setAccessible(true);
                                }
                        }
                        gc1.print();
                        System.out.println();
                }

        }


}




打印结果:
查看是否允许带有可变数量的参数:public Test.GetConstructorsDem(java.lang.String[]) throws java.lang.NumberFormatExceptiontrue
该构造函数的入口参数类型依次为:
class [Ljava.lang.String;
该构造函数可能抛出的一场类型为:
class java.lang.NumberFormatException
s=null
a=100
b=200
c=300

查看是否允许带有可变数量的参数:protected Test.GetConstructorsDem(java.lang.String,int)false
该构造函数的入口参数类型依次为:
class java.lang.String
int
该构造函数可能抛出的一场类型为:
s=7
a=5
b=0
c=0

查看是否允许带有可变数量的参数:private Test.GetConstructorsDem()false
该构造函数的入口参数类型依次为:
该构造函数可能抛出的一场类型为:
s=null
a=0
b=0
c=0
为什么打印结果是先public GetConstructorsDem(String...string),然后protected GetConstructorsDem(String s,int a),最后是private GetConstructorsDem(),不应该是反过来的顺序吗?

先行谢过!

1 个回复

倒序浏览
刚发完就看明白了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马