getConstructor()与getDeclaredConstructor()方法的区别及setAccessible()方法的作用(超详细)

今天在使用反射创建java对象的时候,遇到了点小问题,查看API文档关于getConstructor()和getDeclaredConstructor()方法写的解释模糊不清,就写了几个测试,验证了getConstructor()与getDeclaredConstructor()方法的区别:
首先上代码:

@Getter@Setter
public class HelloWorld {

	private String username;
	
	private HelloWorld() {
		
	}
	
	public void sayHello() {
		System.out.println("欢迎来到王者荣耀:"+username);
	}
}

在这里,为了达到验证的效果,故意将HelloWorld类的构造器私有化.接着使用Junit创建了一个测试方法testName
首先使用getConstructor()方法获取构造器并创建对象,并不使用setAccessible()方法:

@Test
	void testName() throws Exception {
		HelloWorld world=null;
		String className="hello.HelloWorld";
		Constructor con=Class.forName(className).getConstructor();
		Object obj=con.newInstance();
		world=(HelloWorld) obj;
		world.sayHello();
	}

结果意料之中,报java.lang.NoSuchMethodException 错:

在这里插入图片描述
接着,在上面的基础上加上setAccessible()方法:

@Test
	void testName() throws Exception {
		HelloWorld world=null;
		String className="hello.HelloWorld";
		Constructor con=Class.forName(className).getConstructor();
		con.setAccessible(true);
		Object obj=con.newInstance();
		world=(HelloWorld) obj;
		world.sayHello();
	}

运行发现还是同样的配方,还是同样的味道:java.lang.NoSuchMethodException
在这里插入图片描述
接下来,试一试使用getDeclaredConstructor()方法获取构造器并创建对象,不使用setAccessible()方法:

@Test
	void testName() throws Exception {
		HelloWorld world=null;
		String className="hello.HelloWorld";
		Constructor con=Class.forName(className).getDeclaredConstructor();
		Object obj=con.newInstance();
		world=(HelloWorld) obj;
		world.sayHello();
	}

结果让我有些失望,依然是报错,但这次是java.lang.IllegalAccessException异常:

在这里插入图片描述
但是通过IllegalAccessException的字面意思,感觉有戏,再试:

@Test
	void testName() throws Exception {
		HelloWorld world=null;
		String className="hello.HelloWorld";
		Constructor con=Class.forName(className).getDeclaredConstructor();
		con.setAccessible(true);
		Object obj=con.newInstance();
		world=(HelloWorld) obj;
		world.sayHello();
	}

结果果然没让人失望,控制台打印出了结果:
在这里插入图片描述
通过此次测试可以总结出:

  1. Class类的getConstructor()方法,无论是否设置setAccessible(),都不可获取到类的私有构造器.
  2. Class类的getDeclaredConstructor()方法,可获取到类的私有构造器(包括带有其他修饰符的构造器),但在使用时private的构造器时,必须设置setAccessible()为true,才可以获取并操作该Constructor对象。

所以,以后在用反射创建一个私有化构造器类的对象时,务必要用getDeclaredConstructor()方法并设置构造器可访问setAccessible(true)

猜你喜欢

转载自blog.csdn.net/iwlnner/article/details/99674547