A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 灵感 高级黑马   /  2012-9-5 17:10  /  1931 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 严学韦 于 2012-9-6 08:03 编辑

package packge1;

import java.lang.reflect.Constructor;

public class ReflectTest {
public static void main(String[] args) throws Exception{
Constructor constructor1 = String.class.getConstructor(StringBuffer.class);
                String str2 = (String)constructor1.newInstance(new StringBuffer("abc"));
                System.out.println(str2);
     }
}
这里的newInstance张老师讲了不同版本的区别,但是不知道什么意思,求指点一下

评分

参与人数 1技术分 +1 收起 理由
田建 + 1

查看全部评分

3 个回复

倒序浏览
newInstance(Object... initargs)
          使用此 Constructor 对象表示的构造方法来创建该构造方法的声明类的新实例,并用指定的初始化参数初始化该实例。

API上说的官方化,不明白呀
回复 使用道具 举报
一个是最基本的,其他的都是调用它的,看看源码。
  public T newInstance()
        throws InstantiationException, IllegalAccessException
    {
        if (System.getSecurityManager() != null) {
            checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
        }return newInstance0();
    }

       
    private T newInstance0()
        throws InstantiationException, IllegalAccessException
    {
        // NOTE: the following code may not be strictly correct under
        // the current Java memory model.

        // Constructor lookup
        if (cachedConstructor == null) {
            if (this == Class.class) {
                throw new IllegalAccessException(
                    "Can not call newInstance() on the Class for java.lang.Class"
                );
            }
            try {
                Class[] empty = {};
                final Constructor<T> c = getConstructor0(empty, Member.DECLARED);//通过这一步可以看出他是调用Constructor里的获取构造方法,用来获取实例。
                // Disable accessibility checks on the constructor
                // since we have to do the security check here anyway
                // (the stack depth is wrong for the Constructor's
                // security check to work)
                java.security.AccessController.doPrivileged
                    (new java.security.PrivilegedAction() {
                            public Object run() {
                                c.setAccessible(true);
                                return null;
                            }
                        });
                cachedConstructor = c;
            } catch (NoSuchMethodException e) {
                throw new InstantiationException(getName());
            }
        }

评分

参与人数 1技术分 +1 收起 理由
田建 + 1

查看全部评分

回复 使用道具 举报
newInstance(Object... initargs)
Object... initargs可变参数,参数类型是Object类型的,可变参数可以为空,即表示这个是调用空参构造函数。
Constructor constructor1 = String.class.getConstructor(StringBuffer.class);//constructor1表示的是String(StringBuffer bf)这个构造函数,StringBuffer.class 指定了构造函数的参数类型 String str2 = (String)constructor1.newInstance(new StringBuffer("abc"));//这一句相当于 new String(new StringBuffer("abc")), 实例化ConStructor1的声明类String,   new StringBuffer("abc")是指定的初始化参数
    创建该构造方法的声明类的新实例,并用指定的初始化参数初始化该实例。 这句话用以上解释。
    个别参数会自动解包,以匹配基本形参,必要时,基本参数和引用参数都要进行方法调用转换。 这句话的意思就是说,如果你的构造函数里面的参数是基本类型数据 如构造函数conStructor(int x),但是newInstance(Object... initargs) 只接受的Object类型,即Integer类型。根据装箱拆箱原理知,这里面存在拆箱过程。



评分

参与人数 1技术分 +1 收起 理由
田建 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马