[英]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
返回所有构造函数( public
, protected
,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.