newInstance参数详解
来源:互联网 发布:sql带参数的存储过程 编辑:程序博客网 时间:2024/06/06 07:45
通过反射创建新的类示例,有两种方式:
Class.newInstance()
Constructor.newInstance()
以下对两种调用方式给以比较说明:
Class.newInstance() 只能够调用无参的构造函数,即默认的构造函数;
Constructor.newInstance() 可以根据传入的参数,调用任意构造构造函数。
Class.newInstance() 抛出所有由被调用构造函数抛出的异常。
Class.newInstance() 要求被调用的构造函数是可见的,也即必须是public类型的;
Constructor.newInstance() 在特定的情况下,可以调用私有的构造函数。
Class A(被调用的示例):
Java代码
public class A {
private A() {
System.out.println("A's constructor is called.");
}
private A(int a, int b) {
System.out.println("a:" + a + " b:" + b);
}
}
Class B(调用者):
Java代码
public class B {
public static void main(String[] args) {
B b=new B();
out.println("通过Class.NewInstance()调用私有构造函数:");
b.newInstanceByClassNewInstance();
out.println("通过Constructor.newInstance()调用私有构造函数:");
b.newInstanceByConstructorNewInstance();
}
/*通过Class.NewInstance()创建新的类示例*/
private void newInstanceByClassNewInstance(){
try {/*当前包名为reflect,必须使用全路径*/
A a=(A)Class.forName("reflect.A").newInstance();
} catch (Exception e) {
out.println("通过Class.NewInstance()调用私有构造函数【失败】");
}
}
/*通过Constructor.newInstance()创建新的类示例*/
private void newInstanceByConstructorNewInstance(){
try {/*可以使用相对路径,同一个包中可以不用带包路径*/
Class c=Class.forName("A");
/*以下调用无参的、私有构造函数*/
Constructor c0=c.getDeclaredConstructor();
c0.setAccessible(true);
A a0=(A)c0.newInstance();
/*以下调用带参的、私有构造函数*/
Constructor c1=c.getDeclaredConstructor(new Class[]{int.class,int.class});
c1.setAccessible(true);
A a1=(A)c1.newInstance(new Object[]{5,6});
} catch (Exception e) {
e.printStackTrace();
}
}
}
输入结果如下:
通过Class.NewInstance()调用私有构造函数:
通过Class.NewInstance()调用私有构造函数【失败】
通过Constructor.newInstance()调用私有构造函数:
A's constructor is called.
a:5 b:6
说明方法newInstanceByClassNewInstance调用失败,而方法newInstanceByConstructorNewInstance则调用成功。
如果被调用的类的构造函数为默认的构造函数,采用Class.newInstance()则是比较好的选择,
一句代码就OK;如果是老百姓调用被调用的类带参构造函数、私有构造函数,
就需要采用Constractor.newInstance(),两种情况视使用情况而定。
不过Java Totorial中推荐采用Constractor.newInstance()。
Class.newInstance()
Constructor.newInstance()
以下对两种调用方式给以比较说明:
Class.newInstance() 只能够调用无参的构造函数,即默认的构造函数;
Constructor.newInstance() 可以根据传入的参数,调用任意构造构造函数。
Class.newInstance() 抛出所有由被调用构造函数抛出的异常。
Class.newInstance() 要求被调用的构造函数是可见的,也即必须是public类型的;
Constructor.newInstance() 在特定的情况下,可以调用私有的构造函数。
Class A(被调用的示例):
Java代码
public class A {
private A() {
System.out.println("A's constructor is called.");
}
private A(int a, int b) {
System.out.println("a:" + a + " b:" + b);
}
}
Class B(调用者):
Java代码
public class B {
public static void main(String[] args) {
B b=new B();
out.println("通过Class.NewInstance()调用私有构造函数:");
b.newInstanceByClassNewInstance();
out.println("通过Constructor.newInstance()调用私有构造函数:");
b.newInstanceByConstructorNewInstance();
}
/*通过Class.NewInstance()创建新的类示例*/
private void newInstanceByClassNewInstance(){
try {/*当前包名为reflect,必须使用全路径*/
A a=(A)Class.forName("reflect.A").newInstance();
} catch (Exception e) {
out.println("通过Class.NewInstance()调用私有构造函数【失败】");
}
}
/*通过Constructor.newInstance()创建新的类示例*/
private void newInstanceByConstructorNewInstance(){
try {/*可以使用相对路径,同一个包中可以不用带包路径*/
Class c=Class.forName("A");
/*以下调用无参的、私有构造函数*/
Constructor c0=c.getDeclaredConstructor();
c0.setAccessible(true);
A a0=(A)c0.newInstance();
/*以下调用带参的、私有构造函数*/
Constructor c1=c.getDeclaredConstructor(new Class[]{int.class,int.class});
c1.setAccessible(true);
A a1=(A)c1.newInstance(new Object[]{5,6});
} catch (Exception e) {
e.printStackTrace();
}
}
}
输入结果如下:
通过Class.NewInstance()调用私有构造函数:
通过Class.NewInstance()调用私有构造函数【失败】
通过Constructor.newInstance()调用私有构造函数:
A's constructor is called.
a:5 b:6
说明方法newInstanceByClassNewInstance调用失败,而方法newInstanceByConstructorNewInstance则调用成功。
如果被调用的类的构造函数为默认的构造函数,采用Class.newInstance()则是比较好的选择,
一句代码就OK;如果是老百姓调用被调用的类带参构造函数、私有构造函数,
就需要采用Constractor.newInstance(),两种情况视使用情况而定。
不过Java Totorial中推荐采用Constractor.newInstance()。
0 0
- newInstance参数详解
- newInstance() 的参数版本与无参数版本详解
- newInstance() 的参数版本与无参数版本详解
- new()与newInstance() 的参数版本与无参数版本详解
- newInstance()
- Android使用newInstance()实例化fragment&setArguments()传递参数
- java 反射机制 之 newInstance 执行无参数构造函数
- Date newInstance
- new ,newInstance()
- Class.newInstance()
- java newinstance
- Class.newInstance()
- java 反射机制 之 getConstructor获取有参数构造函数 然后newInstance执行有参数的构造函数
- class.newInstance和constructor.newInstance 区别
- new和newInstance() Constructor.newInstance()区别
- Class.newInstance与Constructor.newInstance对比
- new和newInstance() Constructor.newInstance()区别
- newInstance()的问题
- 浅尝KMP算法
- 在linux上安装postgresql
- Linux源码中的mktime算法解析
- KMP算法的Next数组详解
- document.getElementById的用法
- newInstance参数详解
- 【poj2229】Sumsets
- UVa 12700 - Banglawash
- Jquery 中两个页面载入后执行的函数的分析区别
- maven TestNg 测试框架 not found
- 黑马程序员--线程
- 正式宣布DXBC2GLSL,HLSL字节码到GLSL的编译器
- metasploit渗透测试笔记(内网渗透篇)
- java面向对象