亲宝软件园·资讯

展开

Java反射创建对象 详解Java反射创建对象

星夜孤帆 人气:0
想了解详解Java反射创建对象的相关内容吗,星夜孤帆在本文为您仔细讲解Java反射创建对象的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Java反射创建对象,java反射,下面大家一起来学习吧。

一、通过类对象调用newInstance()方法,适用于无参构造方法

1.1 类名.class

public class Main {
 
    public static void main(String[] args) throws IllegalAccessException, InstantiationException {
        Class<Person> clazz = Person.class;
        Person person = clazz.newInstance();
        System.out.println(person instanceof Person); // true
    }
}
 
class Person {
 
    private Integer age;
 
    private String name;
 
    public Person() {
    }
}

1.2 Class.forName

public class Main {
 
    public static void main(String[] args) throws IllegalAccessException, InstantiationException, ClassNotFoundException {
        Class<?> clazz = Class.forName("com.best.test.Person");
        Person person = (Person) clazz.newInstance();
        System.out.println(person instanceof Person); // true
    }
}
 
class Person {
 
    private Integer age;
 
    private String name;
 
    public Person() {
    }
}

1.3 对象名.getClass

public class Main {
 
    public static void main(String[] args) throws IllegalAccessException, InstantiationException{
        Person person = new Person();
        Class<? extends Person> clazz = person.getClass();
        Person person1 = clazz.newInstance();
        System.out.println(person1 instanceof Person); // true
    }
}
 
class Person {
 
    private Integer age;
 
    private String name;
 
    public Person() {
    }
}

二、getConstructor()和getDeclaredConstructor()

通过类对象的getConstructor()或getDeclaredConstructor()方法获得构造器(Constructor)对象并调用其newInstance()方法创建对象,适用于无参和有参构造方法。

2.1 getConstructor()

public class Main {
 
    public static void main(String[] args) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        Class<Person> clazz = Person.class;
        Constructor<Person> ctor = clazz.getConstructor(Integer.class, String.class);
        Person person = ctor.newInstance(26, "jak");
        System.out.println(person instanceof Person); // true
    }
}
 
class Person {
 
    private Integer age;
 
    private String name;
 
    public Person(Integer age, String name) {
        this.age = age;
        this.name = name;
    }
}

2.2 getDeclaredConstructor()

public class Main {
 
    public static void main(String[] args) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        Class<Person> clazz = Person.class;
        Constructor<Person> ctor = clazz.getDeclaredConstructor(String.class);
        Person person = ctor.newInstance("jak");
        
        System.out.println(person instanceof Person); // true
    }
}
 
class Person {
 
    private Integer age;
 
    private String name;
 
    public Person(Integer age, String name) {
        this.age = age;
        this.name = name;
    }
 
    public Person(String name) {
        this.name = name;
    }
}

2.3 getConstructor()和getDeclaredConstructor()区别

getDeclaredConstructor(Class<?>... parameterTypes) 

这个方法会返回指定参数类型的所有构造器,包括public的和非public的,当然也包括private的。getDeclaredConstructors()的返回结果就没有参数类型的过滤了。

再来看getConstructor(Class<?>... parameterTypes)

这个方法返回的是上面那个方法返回结果的子集,只返回指定参数类型访问权限是public的构造器。getConstructors()的返回结果同样也没有参数类型的过滤。

参考文章

加载全部内容

相关教程
猜你喜欢
用户评论