简体   繁体   English

从类获取方法,然后通过对象调用

[英]Get method from Class then invoke via Object

In order to invoke a method on a object via reflection, the only way I know how to do it is like so: 为了通过反射在对象上调用方法,我知道怎么做的唯一方法是这样的:

Object o = ...;
Method m = o.getClass().getMethod("methodName",null);
Object x = m.invoke(o,null);

Why doesn't Java have a getMethods method in the Object class? Java为什么在Object类中没有getMethods方法? (As well as getSuperClass, getFields, etc, etc). (以及getSuperClass,getFields等)。

So we could just do something like: 所以我们可以做类似的事情:

Object x = o.invoke("methodName",null);

Why not? 为什么不? I assume this is for performance reasons. 我认为这是出于性能原因。

(Also as a side note. In English, it makes more sense to say 'subject invokes object', so that would be in programming terms, object invoke method. But with Java we get 'method invoke on object'. Glad I could confuse you today.) (也作为附带说明。用英语来说,“主题调用对象”更有意义,所以用编程术语来说就是对象调用方法。但是使用Java时,我们得到了“对象上方法调用”。很高兴让我感到困惑你今天。)

I believe the reason is that java.lang.Object is supposed to serve as the root of the class hierarchy and any method on it should pertain to instances of that object rather than the concept of an object. 我认为原因是应该将java.lang.Object用作类层次结构的根,并且其上的任何方法都应属于该对象的实例 ,而不是对象的概念。

Adding reflection utility methods to Object would spoil this. Object添加反射实用程序方法会破坏这一点。 You'd have a choice of calling o.myMethod() or o.invoke("myMethod", null) and this would introduce a style of programming in Java that is not compile-safe, in that there is no compile-time guarantee that "myMethod" exists in the latter. 您可以选择调用o.myMethod()o.invoke("myMethod", null) ,这将引入Java中不安全编译的编程风格,因为没有编译时保证后者中存在“ myMethod”。 This would make it very easy for developers to do away with type safety and just use .invoke all the time without bothering to consider proper object-oriented design. 这将使开发人员很容易放弃类型安全性,而.invoke使用.invoke而不用考虑适当的面向对象设计。

By forcing developers to explicitly ask for an instance of Class , we maintain this separation between the reflection API and "concrete" Java. 通过强迫开发人员明确要求Class的实例,我们在反射API和“具体” Java之间保持了这种分离。 So, while it can sometimes be a pain, it's good to encourage developers to code properly. 因此,尽管有时可能会很痛苦,但最好鼓励开发人员正确编写代码。 Also, it means that the OOP concept of an object is represented by java.lang.Object and the concept of a class is represented by java.lang.Class , which is a nice, clear distinction of responsibility. 同样,这意味着对象的OOP概念由java.lang.Object表示,而类的概念由java.lang.Class表示,这是对职责的一种很好的清晰区分。

The class Object Object

is the root of the class hierarchy. 是类层次结构的根。

It describes behavior that every class will need. 它描述了每个类将需要的行为。

Any additional behavior is described by the sub classes, but a sub class doesn't necessarily have to have behavior. 子类描述了任何其他行为,但是子类不一定必须具有行为。 You can declare a class of constants, an enum , or even an array. 您可以声明一类常量,一个enum甚至一个数组。 Then it wouldn't make sense to have an invoke method on those. 然后,在这些对象上具有invoke方法是没有意义的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM