我可以使用 Java Reflection 获取所有构造函数(私有(private)、 protected 和公共(public)):
public Constructor<?>[] getDeclaredConstructors();
如何仅获取 java 类的 protected 和公共(public)构造函数?
最佳答案
getConstructors()
返回公共(public)构造函数。要获得 protected 构造函数,您必须使用 getDeclaredConstructors() ,然后迭代数组并检查构造函数是否 protected 。
这是代码示例:
for (Constructor c : clazz.getDeclaredConstructors()) {
if (Modifier.isProtected(c.getModifiers())) {
// this constructor is protected
}
}
关于java - 如何仅获取 java 类的 protected 和公共(public)构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28671943/