简体   繁体   English

如何通过类引用访问静态方法

[英]How to access a static method via a class reference

class A {
    public static void foo() {}
}

class B {
    public static void foo() {}
}

I have Class clazz = A.class; or B.class我有Class clazz = A.class; or B.class Class clazz = A.class; or B.class ; Class clazz = A.class; or B.class ;

How do I access this via "clazz" assuming it might be assigned either 'A' or 'B'假设它可能被分配了“A”或“B”,我如何通过“clazz”访问它

It is only possible to access those methods using reflection.只能使用反射访问这些方法。 You cannot reference a class directly, only an instance of type Class.你不能直接引用一个类,只能引用一个 Class 类型的实例。

To use reflection to invoke methodname(int a, String b):要使用反射调用 methodname(int a, String b):

Method m = clazz.getMethod("methodname", Integer.class, String.class);
m.invoke(null, 1, "Hello World!");

See Class.getMethod() and Method.invoke()参见Class.getMethod()Method.invoke()

You may want to think about your design again, to avoid the need to dynamically call static methods.您可能需要重新考虑您的设计,以避免需要动态调用静态方法。

You can invoke a static method via reflection like this :您可以像这样通过反射调用静态方法:

Method method = clazz.getMethod("methodname", argstype);
Object o = method.invoke(null, args);

Where argstype is an array of arguments type and args is an array of parameters for the call.其中 argstype 是参数类型的数组,args 是调用的参数数组。 More informations on the following links :有关以下链接的更多信息:

In your case, something like this should work :在你的情况下,这样的事情应该工作:

Method method = clazz.getMethod("foo", null);
method.invoke(null, null); // foo returns nothing

You cannot access static methods without an explicit reference to the class.如果没有对类的显式引用,您将无法访问静态方法。

No inheritance here, sorry, so you must either do:这里没有继承,抱歉,所以你必须这样做:

A.foo()

or或者

B.foo()

If you really need it, you will have to do a check:如果你真的需要它,你将不得不做一个检查:

Object o = .... // eith an A or B instance.
if( o instanceof A ) {
    A.foo()
} else {
    B.foo()
}

But why don't you just make those functions instance functions, and let them implement an interface?但是你为什么不把这些函数作为实例函数,让它们实现一个接口呢?

Okey, you have a class object.好吧,你有一个类对象。 Then do:然后做:

Class c = ...;
c.getMethod("foo").invoke(null); // null to invoke static methods

According to my lack of knowledge the need for the requested construct is given by the fact that an interface doesn't offer the possibility of static abstract methods.根据我的知识缺乏,接口不提供静态抽象方法的可能性这一事实给出了对所请求构造的需求。 Here is an example:下面是一个例子:

public enum Cheese implements Yumy {
  GOUDA(49),
  ESROM(40),
  HWARTI(38);
  private int percentage;
  private Cheese(int fat100) {...} constructor
  public void yamyam() {...} // as in Yumy
  public static Cheese getByFat(int fat100) {...} // no chance to be part
                                                     of interface
};

I hope this isn't making too many assumptions or deviating too far from your question, but if your two classes share a common supertype and creating an instance is tolerable then you can:我希望这不会做出太多假设或偏离您的问题太远,但是如果您的两个类共享一个共同的超类型并且创建一个实例是可以容忍的,那么您可以:

  1. Implement a common interface实现通用接口
  2. Create an instance of the object via myClass.newInstance() (class must have an empty constructor)通过myClass.newInstance()创建对象的实例(类必须有一个空的构造函数)
  3. Call the static method from the instance object.从实例对象调用静态方法。


interface Foo {
    void foo();
}
class A implements Foo {...}
class B implements Foo {...}

<T extends Foo> public void something(Class<T> clazz) {
    T myInstance = clazz.newInstance();
    myInstance.foo();
}

...
something(A.class);

It's a little bizarre but in my case it proved to be useful, and I began by asking the very same question that you did.这有点奇怪,但在我的情况下它被证明是有用的,我首先问了和你一样的问题。

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

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