美文网首页
Java 反射学习《二》 Constructor详解

Java 反射学习《二》 Constructor详解

作者: lq_ios | 来源:发表于2020-11-02 16:09 被阅读0次

Java 反射学习《一》 Field详解

Java 反射学习《二》 Constructor详解

Java 反射学习《三》Method详解

根据Class获取类的构造方法

  • public Constructor<?>[] getDeclaredConstructors() throws SecurityException 获取类的所有构造方法。这些构造方法可以使用 public, protected, 默认,private修饰。这些构造方法是无序的,如果类有默认的构造方法,他也会包含在数组中。这个这个Class表示的是接口primitive类型,还有数组,这个方法无效。

  • public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)throws NoSuchMethodException, SecurityException 根据指定参数获取构造方法。这些构造方法可以使用 public, protected, 默认,private修饰。

  • public Constructor<?>[] getConstructors() throws SecurityException 获取类的所有public修饰的构造函数

  • public Constructor<T> getConstructor(Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException 根据指定参数获取到相应的public构造方法

public class Person{
    public String name;
    public int age;

    public Person() {
        this("这是默认值");
    }

    protected Person(String name) {
        this("小明", 10);
    }

    private Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public <T,U> Person(T name,U age){
        System.out.println("这是构造函数");
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

  Class pClass = Person.class;
  Constructor[] constructors = pClass.getDeclaredConstructors();
  for (Constructor con : constructors) {
      System.out.println("修饰符:" + Modifier.toString(con.getModifiers()) +
              " 名称:" + con.getName() +
              " 参数个数" + con.getParameterCount());
  }
  System.out.println("*******************************");
  Constructor[] pubConstructors = pClass.getConstructors();
  for (Constructor con : pubConstructors) {
      System.out.println("修饰符:" + Modifier.toString(con.getModifiers()) +
              " 名称:" + con.getName() +
              " 参数个数" + con.getParameterCount());
  }

//输出
//修饰符:public 名称:com.company.Person 参数个数2
//修饰符:private 名称:com.company.Person 参数个数2
//修饰符:protected 名称:com.company.Person 参数个数1
//修饰符:public 名称:com.company.Person 参数个数0
//*******************************
//修饰符:public 名称:com.company.Person 参数个数2
//修饰符:public 名称:com.company.Person 参数个数0

Constructor类的常用方法

  1. public Class<T> getDeclaringClass() 声明该构造函数的类

  2. public String getName() 构造函数的名称

  3. public int getModifiers() 构造函数 所使用的修饰符

  4. public Parameter[] getParameters() 获取构造函数的参数

  5. public TypeVariable<Constructor<T>>[] getTypeParameters() 此方法返回TypeVariable对象的数组,这些对象表示修饰泛型参数的类型。

     public <T,U> Person(T name,U age){
            System.out.println("这是构造函数");
     }
     
      TypeVariable[] variable = con.getTypeParameters();
      for (TypeVariable type :variable) {
            System.out.println(type.getName());
      }
      //输出
      //T
      //U
    
  6. public Class<?>[] getParameterTypes() 获取构造函数参数的类型

  7. public int getParameterCount() 获取构造函数参数的个数

  8. public Type[] getGenericParameterTypes() 获取泛型参数类型

  9. public Class<?>[] getExceptionTypes() 返回该构造函数可能抛出的异常类型

  10. public boolean isVarArgs() 是否允许带有可变数量的参数

  11. public boolean isSynthetic() 是否是合成构造函数

  12. public Annotation[] getDeclaredAnnotations() 获取所有注解

  13. public T newInstance(Object ... initargs)throws InstantiationException,IllegalAccessException,IllegalArgumentException, InvocationTargetException 通过该构造方法创建对象

    try {
        Class pClass = Person.class;
        //这个构造函数式private的,需要setAccessible(true),否则会抛java.lang.IllegalAccessException
        Constructor constructor = pClass.getDeclaredConstructor(String.class,int.class);
        constructor.setAccessible(true);
        Person person = (Person) constructor.newInstance("小白",30);
        System.out.println(person);
    }catch (Exception e) {
        System.out.println(e.fillInStackTrace());
    }
    //输出
    //Person{name='小白', age=30}
    

Parameter的常用方法

  1. public int getModifiers() 获取参数的修饰符
  2. public String getName() 获取参数名
  3. public Class<?> getType() 获取参数类型

Modifier的常用方法

  1. public static boolean isPublic(int mod) 是否使用public修饰
  2. public static boolean isPrivate(int mod) 是否使用private修饰
  3. public static boolean isProtected(int mod) 是否使用protected 修饰
  4. public static boolean isStatic(int mod) 是否使用static 修饰
  5. public static boolean isFinal(int mod)是否使用final 修饰
  6. public static boolean isSynchronized(int mod) 是否使用synchronized修饰
  7. public static boolean isVolatile(int mod) 是否使用volatile修饰
  8. public static boolean isTransient(int mod) 是否使用transient修饰
  9. public static boolean isNative(int mod) 是否使用native修饰
  10. public static boolean isVolatile(int mod) 是否使用volatile修饰
  11. public static boolean isInterface(int mod) 是否使用interface修饰
  12. public static boolean isAbstract(int mod) 是否使用abstract修饰
  13. public static boolean isStrict(int mod) 是否使用strictfp修饰

相关文章

网友评论

      本文标题:Java 反射学习《二》 Constructor详解

      本文链接:https://www.haomeiwen.com/subject/zpymvktx.html