其他分享
首页 > 其他分享> > 反射创建实例对象

反射创建实例对象

作者:互联网

第一步:Constructor 对象获取(更多请查看JDK文档,关键字:Class)

// 学生类
public class Student { private String name; int age; protected String address; public String tel; // getter 和 setter 方法 public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } // 成员方法 void eat(){ System.out.println("吃东西"); } protected void sleep(){ System.out.println("睡觉"); } private void study(String subject){ System.out.println("学" + subject); } // 构造方法 public Student() {}  // 构0
Student(String name, int age) {  // 构2 this.name = name; this.age = age; }
protected Student(String name, int age, String address) {  // 构3 this.name = name; this.age = age; this.address = address; }
private Student(String name, int age, String address, String tel) {  // 构4 this.name = name; this.age = age; this.address = address; this.tel = tel; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", address='" + address + '\'' + ", tel='" + tel + '\'' + '}'; } }

import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.util.Properties;

public class Test {
    public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException {

        // 1.读取需要被加载进内存的类的路径
        Properties properties = new Properties();
        FileInputStream fis = new FileInputStream("day14\\class.properties");  // 读取配置文件
        properties.load(fis);
        //2.从集合中,把className的信息获取到
        String className = properties.getProperty("className");
        //3.反射根据类的全路径(包名 + 类名),将当前的类加载进内存,形参Class类对象
        Class<?> c = Class.forName(className);

        System.out.println("Constructor<?>[] getConstructors()  获取所有的公共的构造方法");
        Constructor<?>[] constructors = c.getConstructors();
        for (Constructor<?> constructor : constructors) {
            System.out.println(constructor);
        }

        System.out.println("Constructor<?>[] getDeclaredConstructors() 获取所有的构造方法");
        Constructor<?>[] declaredConstructors = c.getDeclaredConstructors();
        for (Constructor<?> declaredConstructor : declaredConstructors) {
            System.out.println(declaredConstructor);
        }
System.out.println("Constructor<T> getConstructor(Class<?>... parameterTypes) 获取单个的公共的构造方法对象"); Constructor<?> constructor = c.getConstructor(); // 获取了一个公共的,空参的构造方法(构0) System.out.println(constructor); System.out.println("Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) 获取单个的,任意权限修饰符的构造方法对象 (推荐使用)"); // 构0 Constructor<?> declaredConstructor0 = c.getDeclaredConstructor(); System.out.println(declaredConstructor0); // 构2 Constructor<?> declaredConstructor2 = c.getDeclaredConstructor(String.class, int.class); System.out.println(declaredConstructor2); // 构3 Constructor<?> declaredConstructor3 = c.getDeclaredConstructor(String.class, int.class, String.class, String.class); System.out.println(declaredConstructor3); fis.close(); // 关流 } }
测试结果如下:

Constructor<?>[] getConstructors() 获取所有的公共的构造方法
public cn.powernode.javase.reflect.constructor.Student()
Constructor<?>[] getDeclaredConstructors() 获取所有的构造方法
public cn.powernode.javase.reflect.constructor.Student()
protected cn.powernode.javase.reflect.constructor.Student(java.lang.String,int,java.lang.String)
private cn.powernode.javase.reflect.constructor.Student(java.lang.String,int,java.lang.String,java.lang.String)
cn.powernode.javase.reflect.constructor.Student(java.lang.String,int)
Constructor<T> getConstructor(Class<?>... parameterTypes) 获取单个的公共的构造方法对象
public cn.powernode.javase.reflect.constructor.Student()
Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) 获取单个的,任意权限修饰符的构造方法对象 (推荐使用)
public cn.powernode.javase.reflect.constructor.Student()
cn.powernode.javase.reflect.constructor.Student(java.lang.String,int)
private cn.powernode.javase.reflect.constructor.Student(java.lang.String,int,java.lang.String,java.lang.String)

-----------------------------------------------------

第二步:根据获取到的 Constructor 对象,创建该类的实例对象

 

标签:反射,String,创建,age,实例,Student,Constructor,public,name
来源: https://www.cnblogs.com/lwj0126/p/16479594.html