zoukankan      html  css  js  c++  java
  • Java Class.newInstance()方法

    只有当类具有默认的构造器时,才能调用Class.newInstance();否则抛异常:具体执行情况可以调试进去看看。

    ————————————————————————————————————————

    at java.lang.Class.newInstance0(Class.java:340)

    at java.lang.Class.newInstance(Class.java:308)

    ———————————————————————————————————————— 

     下面是关键代码调用:

    public T newInstance() throws InstantiationException, IllegalAccessException

     {
            if (System.getSecurityManager() != null) {  
               checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
            }
            return newInstance0();
     }
    private T newInstance0()  throws InstantiationException, IllegalAccessException 

    {

           ............

           try {

                    Class[] empty = {};

                    final Constructor<T> c = getConstructor0(empty, Member.DECLARED); 

                    ................ 

           }catch (NoSuchMethodException e) {

                    throw new InstantiationException(getName());

           } 

    private Constructor<T> getConstructor0(Class[] parameterTypes,  int which) throws NoSuchMethodException
     {
            Constructor[] constructors = privateGetDeclaredConstructors((which == Member.PUBLIC));
            for (int i = 0; i < constructors.length; i++) {
                if (arrayContentsEq(parameterTypes,  constructors[i].getParameterTypes())) {
                    return getReflectionFactory().copyConstructor(constructors[i]);
                }
            }
            throw new NoSuchMethodException(getName() + ".<init>" + argumentTypesToString(parameterTypes));

     } 

  • 相关阅读:
    字符串方法
    函数的属性和方法
    数组的去重!!
    常见的数组方法
    JS中的函数
    JavaScript 中表达式和语句的区别
    运算符优先级
    题解 CF813B 【The Golden Age】
    题解 CF834B 【The Festive Evening】
    题解 CF810B 【Summer sell-off】
  • 原文地址:https://www.cnblogs.com/xinglongbing/p/1999305.html
Copyright © 2011-2022 走看看