六十五、JAVA创建对象的五种方式

版权声明:转载请注明:https://blog.csdn.net/u010285974 https://blog.csdn.net/u010285974/article/details/86705940

 反射创建对象+正常创建对象

package com.daquan.reflect;

import com.daquan.user.Person;
import com.daquan.user.User;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/*
 *功能:
 *@author:zhangdaquan
 *@date:2019/1/30 下午2:15
 *@version:1.0
 */
public class TestReflect<T> {
    /*
     * 功能:创建User对象
     * @author zhangdaquan
     * @date 2019/1/30 下午2:49
     * @param []
     * @return com.daquan.user.User
     * @exception
     */
    public static User getUserInstance(){
        System.out.println("getUserInstance start");
        User user = new User();
        user.setId(0);
        System.out.println("getUserInstance end");
        return user;
    }
    /*
     * 功能:通过类对象创建类的实例
     * @author zhangdaquan
     * @date 2019/1/30 下午2:44
     * @param []
     * @return com.daquan.user.User
     * @exception
     */
    public static User getUserInstance1() throws IllegalAccessException, InstantiationException {
        System.out.println("getUserInstance1 start");
        User user = User.class.newInstance();
        user.setId(1);
        System.out.println("getUserInstance1 end");
        return user;
    }
    /*
     * 功能:通过类名反射出构造方法,然后创建实例
     * @author zhangdaquan
     * @date 2019/1/30 下午2:51
     * @param []
     * @return com.daquan.user.User
     * @exception
     */
    public static User getUserInstance2()
            throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException,
                   InstantiationException {
        System.out.println("getUserInstance2 start");
        Class c = Class.forName(User.class.getName());
        Constructor constructor = c.getConstructor();
        User user = (User) constructor.newInstance();
        user.setId(2);
        System.out.println("getUserInstance2 end");
        return user;
    }
    /*
     * 功能:通过类对象直接拿到构造函数,用构造函数生成实例
     * @author zhangdaquan
     * @date 2019/1/30 下午3:10
     * @param []
     * @return com.daquan.user.User
     * @exception
     */
    public static User getUserInstance3()
            throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        System.out.println("getUserInstance3 start");
        Constructor constructor = User.class.getConstructor();
        User user = (User) constructor.newInstance();
        user.setId(3);
        System.out.println("getUserInstance3 end");
        return user;
    }
    /*
     * 功能:通过类对象拿到类反射器加载类并得到构造器生成对象实例
     * @author zhangdaquan
     * @date 2019/1/30 下午3:10
     * @param []
     * @return com.daquan.user.User
     * @exception
     */
    public static User getUserInstance4()
            throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException,
                   InstantiationException {
        System.out.println("getUserInstance4 start");
        Constructor constructor = User.class.getClassLoader().loadClass(User.class.getName()).getConstructor();
        User user = (User) constructor.newInstance();
        user.setId(4);
        System.out.println("getUserInstance4 end");
        return user;
    }

    public static Object getPersonInstance(Class clazz) throws IllegalAccessException, InstantiationException {
        return clazz.newInstance();
    }

    public static void main(String[] args)
            throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException,
                   ClassNotFoundException {
        Object personInstance = getPersonInstance(Person.class);
        System.out.println(personInstance.toString());
        /*User user;

        System.out.println(User.class);
        user = getUserInstance();
        System.out.println("id:"+user.getId());

        user = getUserInstance1();
        System.out.println("id:"+user.getId());

        user = getUserInstance2();
        System.out.println("id:"+user.getId());

        user = getUserInstance3();
        System.out.println("id:"+user.getId());

        user = getUserInstance4();
        System.out.println("id:"+user.getId());*/

    }
}
package com.daquan.user;

/*
 *功能:
 *@author:zhangdaquan
 *@date:2019/1/30 下午2:15
 *@version:1.0
 */
public class User {

    /*private User(){
    }*/
    static {
        System.out.println("init");
    }

    private int id;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

猜你喜欢

转载自blog.csdn.net/u010285974/article/details/86705940