简体   繁体   English

Class API中getDeclaredConstructors和getConstructors有什么区别?

[英]What is the difference between getDeclaredConstructors and getConstructors in the Class API?

I notice that in the Java Reflection API there are two different methods for invoking constructors: the getDeclaredConstructors / getConstructors method. 我注意到在Java Reflection API中有两种不同的方法来调用构造函数: getDeclaredConstructors / getConstructors方法。 Although the Java docs are slightly different ( getDeclaredConstructors seems to imply that it returns ALL constructors, rather than public ones), its not clear why the API explicitly supports these two different methods. 尽管Java文档略有不同( getDeclaredConstructors似乎暗示它返回所有构造函数而不是公共构造函数),但不清楚为什么API明确支持这两种不同的方法。

More importantly, I'm wondering : when would one method be preferable to another if we are invoking classes dynamically ? 更重要的是,我想知道:如果我们动态调用类,一个方法何时会优于另一个方法? For example, what is the purpose of accessing a private constructor? 例如,访问私有构造函数的目的是什么?

getDeclaredConstructors (when you want all the constructors) getDeclaredConstructors (当你想要所有的构造函数时)

Returns an array of Constructor objects reflecting all the constructors declared by the class represented by this Class object. 返回Constructor对象的数组,这些对象反映由此Class对象表示的类声明的所有构造函数。 These are public, protected, default (package) access, and private constructors. 这些是公共,受保护,默认(包)访问和私有构造函数。

getConstructors (when you want only public constructors) getConstructors (当你想要public构造函数时)

Returns an array containing Constructor objects reflecting all the public constructors of the class represented by this Class object. 返回一个包含Constructor对象的数组,这些对象反映此Class对象所表示的类的所有公共构造函数。

So, looking at the docs for both of them, I think a difference is that getConstructors returns only public constructors while getDeclaredConstructors returns all the constructors ( public , protected , default (package) access, and private ) 因此,查看两者的文档,我认为不同之处在于getConstructors仅返回public构造函数,而getDeclaredConstructors返回所有构造函数( publicprotected ,default(package)access和private

So, it's easy if you need only the public constructors then use getConstructors . 因此,如果您只需要public构造函数然后使用getConstructors ,那么很容易。 Otherwise, if you need all the constructors (disregarding the access-modifier of the constructor) then use getDeclaredConstructors . 否则,如果您需要所有构造函数(忽略构造函数的access-modifier),则使用getDeclaredConstructors

The getDeclaredXX() methods exist for the manipulation of classes in ways that weren't necessarily intended by the maker of those classes. getDeclaredXX()方法用于以不一定由这些类的制造者所预期的方式操作类。 Note that there is a getDeclaredMethod() method that allows you to invoke private methods, and getDeclaredField() method that allows you to get/set private fields. 请注意,有一个getDeclaredMethod()方法允许您调用私有方法, getDeclaredField()方法允许您获取/设置私有字段。

I'm not completely sure about "legitimate" use cases, but these are obviously useful for doing certain things. 我不完全确定“合法”用例,但这些对于做某些事情显然很有用。 Also, this family of methods only returns things specifically declared in the class, not things that exist in the class because of the superclass. 此外,这个方法系列只返回类中特别声明的内容,而不是由于超类而返回类中存在的内容。

Accessing a private constructor could be useful for the same reasons, I suppose. 我想,访问私有构造函数可能会出于同样的原因。

The method 'getDeclaredConstructors' returns only the Constructors that are declared inside the class. 方法'getDeclaredConstructors'仅返回在类中声明的构造函数。 The method 'getConstructors' also returns constructors that are not declared inside the class but are inherited from super classes. 方法'getConstructors'也返回未在类中声明但从超类继承的构造函数。

So it depends what you're going to do. 所以这取决于你要做什么。 There is no right approach, it really depends if you also need super constructors. 没有正确的方法,这取决于你是否还需要超级构造函数。

I use getDeclaredConstructors to create a factory of classes whose constructor is private to avoid that the "users" of my system can create these classes directly without using the factory. 我使用getDeclaredConstructors创建一个类的工厂,其构造函数是私有的,以避免我的系统的“用户”可以直接创建这些类而不使用工厂。 I find this method quite useful. 我发现这种方法非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 loadClass((“完全合格的类名”)和 <ClassName> .class.getDeclaredConstructors - what is the difference between loadClass((“fully qualified class name”) and <ClassName>.class.getDeclaredConstructors Class.getConstructors()在Java中返回的数组顺序如何 - How is the order of the array what Class.getConstructors() returns in Java Class中getConstructors()的泛型类型 - Generic type in getConstructors() in Class Java API中的类“Class”和“class”关键字之间有什么区别? - What is the difference between the class “Class” in the Java API and the “class” keyword? 类中这些字段有什么区别 - What is the difference between these fields in class 肿型和标记型有什么区别? - what is the difference between bloated class and tagged class? “公共课”和“课”有什么区别? - what is the difference between `public class` and just `class`? Netbeans调试器在Class.java.getDeclaredConstructors处停止 - Netbeans debugger stops at Class.java.getDeclaredConstructors JPA2 Criteria API:在from子句中使用Entity.class和Metamodel的EntityType有什么区别 - JPA2 Criteria API: What is the difference between using Entity.class and the EntityType of the Metamodel in the from clause API和Web服务有什么区别? - What is the difference between API and web services?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM