通过反射创建新类示例的两种方式及比较
2010-04-20 11:30
369 查看
通过反射创建新类示例的两种方式及比较
作者BLOG:http://blog.csdn.net/fenglibing
通过反射创建新的类示例,有两种方式:
Class.newInstance()
Constructor.newInstance()
以下对两种调用方式给以比较说明:
l Class.newInstance() 只能够调用无参的构造函数,即默认的构造函数;而Constructor.newInstance() 可以根据传入的参数,调用任意构造构造函数。
l Class.newInstance() 抛出所有由被调用构造函数抛出的异常。
l Class.newInstance() 要求被调用的构造函数是可见的,也即必须是public类型的; Constructor.newInstance() 在特定的情况下,可以调用私有的构造函数。
Class A(被调用的示例):
view plaincopy to clipboardprint?
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);
}
}
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(调用者):
view plaincopy to clipboardprint?
import java.lang.reflect.Constructor;
import static java.lang.System.out;
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();
}
}
}
import java.lang.reflect.Constructor;
import static java.lang.System.out;
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()。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fenglibing/archive/2009/09/08/4531033.aspx
作者BLOG:http://blog.csdn.net/fenglibing
通过反射创建新的类示例,有两种方式:
Class.newInstance()
Constructor.newInstance()
以下对两种调用方式给以比较说明:
l Class.newInstance() 只能够调用无参的构造函数,即默认的构造函数;而Constructor.newInstance() 可以根据传入的参数,调用任意构造构造函数。
l Class.newInstance() 抛出所有由被调用构造函数抛出的异常。
l Class.newInstance() 要求被调用的构造函数是可见的,也即必须是public类型的; Constructor.newInstance() 在特定的情况下,可以调用私有的构造函数。
Class A(被调用的示例):
view plaincopy to clipboardprint?
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);
}
}
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(调用者):
view plaincopy to clipboardprint?
import java.lang.reflect.Constructor;
import static java.lang.System.out;
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();
}
}
}
import java.lang.reflect.Constructor;
import static java.lang.System.out;
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()。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fenglibing/archive/2009/09/08/4531033.aspx
相关文章推荐
- 通过反射创建新类示例的两种方式及比较
- 通过反射创建新类示例的两种方式及比较
- Java多线程中线程的两种创建方式及比较代码示例
- 利用反射机制创建新类的两种方式及比较
- 创建一个类,它包含一个在定义时就被初始化了的String域,以及另一个通过构造器初始化的String域。这两种方式有何差异
- java多线程总结一: 线程的两种创建方式及优劣比较
- 线程的两种创建方式及比较
- 通过物化视图方式同步两服务器中数据表的两种不同方式的比较
- Java反射02 : Class对象获取的三种方式和通过反射实例化对象的两种方式
- 《Spark商业案例与性能调优实战100课》第9课:商业案例之通过Spark SQL 下两种不同方式实现口碑最佳和最热门电影比较
- java多线程总结一: 线程的两种创建方式及优劣比较
- 两种OpenXML创建Excel单元格的方式比较
- java多线程总结一:线程的两种创建方式及优劣比较
- Java多线程总结(一):Java两种创建线程方式的比较
- 线程的两种创建方式及优劣比较
- 通过IntelliJ IDEA创建spring boot 项目的两种方式之方式一
- java多线程总结一: 线程的两种创建方式及优劣比较
- java多线程总结一:线程的两种创建方式及优劣比较
- 多线程1:两种方式创建线程示例
- 线程的两种创建方式及优劣比较