简体   繁体   English

调用类型为Class的对象的方法

[英]Invoke a method of an object of type Class

I ll try to set it as simple as possible because it confuses me too. 我会尝试将其设置得尽可能简单,因为它也会使我感到困惑。

I got a method returning an object of type Class ie 我有一个方法返回Class类型的对象,即

public Class foo(){...}

When I call this method I'm keeping the result in a variable of type Class ie 当我调用此方法时,我会将结果保留在Class类型的变量中,即

Class obj = new foo();

foo() can return many different classes BUT all of them contain the same method bar() . foo()可以返回许多不同的类,但它们都包含相同的方法bar()

How can I invoke that method from my obj variable? 如何从obj变量调用该方法?

I tried obj.bar() but the IDE doesn't seem to like it. 我尝试了obj.bar()但IDE似乎不喜欢它。 I get 我懂了

Error:(59, 34) error: cannot find symbol method bar() 错误:(59,34)错误:找不到符号方法bar()

It seems like you want to group behaviors, that's what Java interfaces do: 似乎您想对行为进行分组,这就是Java接口的作用:

interface Barer {
    String bar();
}

class Foo implements Barer {
    String bar() {
        return "I'm a Foo";
    }
}

class Fooz implements Barer {
    String bar() {
        return "I'm a Fooz";
    }
}

Then in you code you can have something like: 然后在您的代码中可以有类似以下内容:

Barer obj;

if (something) {
    obj = new Foo();
} else {
    obj = new Fooz();
}

obj.bar()

It makes little sense in fact to store the return value of a constructor in a Class object. 实际上,将构造函数的返回值存储在Class对象中几乎没有意义。


If you really are in the case that you need a Class object (remember that it will just point to the Class, not to the instance you have created), you can use Java reflection : 如果确实需要一个Class对象(请记住它只会指向Class而不是您创建的实例),则可以使用Java Reflection

Class obj = new foo();
Method method = obj.getDeclaredMethod("bar");
method.invoke(null);

if method foo() returns Class, it means it don't return actual instance of the objet on which you want to call bar() method, but the class itself, so it's not possible to call method bar() on Class as Class.class don't have a method bar. 如果方法foo()返回Class,则表示它不返回要在其上调用bar()方法的对象的实际实例,而是类本身,因此不可能将Class上的Class方法调用bar() .class没有方法栏。

In your code example there is some mistake 在您的代码示例中有一些错误

Class obj = new foo();

The keyword new mean you're creating a new instance of a class, not you're calling a method on an object. 关键字new表示您正在创建类的新实例,而不是在对象上调用方法。


In fact the right approach would be to use interfaces. 实际上,正确的方法是使用接口。

Instead of returning Class from foo method, declare an interface, 声明接口,而不是从foo方法返回Class,

public interface MyInterface {
  public void bar();
}

and make foo() returning MyInterface instead of Class. 并使foo()返回MyInterface而不是Class。

Then you can do 那你可以做

MyInterface obj = tmp.foo();
obj.bar()

If all returned values are objects of classes that implements the same method, you should declare that method in separate interface and let them implement it. 如果所有返回的值都是实现相同方法的类的对象,则应在单独的接口中声明该方法并让他们实现。 Later on you can use generic wildcard like <? extends BarInterface> 稍后您可以使用通用通配符,例如<? extends BarInterface> <? extends BarInterface> or change return type to BarInterface . <? extends BarInterface>或将返回类型更改为BarInterface

public interface BarInterface{
    public void bar();
}

and

public <T extends BarInterface> foo(){...}

or 要么

public BarInterface foo() {...}

暂无
暂无

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

相关问题 从类获取方法,然后通过对象调用 - Get method from Class then invoke via Object 调用一个返回通用列表类类型的对象的方法将引发java.lang.IllegalArgumentException - invoke a method which is returning an object of generic list class type throws java.lang.IllegalArgumentException 将子类中的对象放入超类类型的数组中后,如何调用子类方法? - How to invoke subclass method after putting an object from subclass in array of type super class? 一种获取类型为Class的对象的方法 - A method for obtaining an object of type Class java-从另一个类返回无效类型的调用方法 - java - invoke method with void return type from another class 如何从具有泛型类型的抽象类中调用方法(使用Java反射) - How To invoke a Method (with java reflection) from abstract class with generic type 无法对数组类型 (Class) MitarbeiterListe[] 调用方法 getSize() - Cannot invoke method getSize() on the array type (Class) MitarbeiterListe[] 调用类中定义的私有对象的方法(Java反射) - Invoke a method of a private object defined in a class (Java reflection) 如何在Android中以final类对象作为参数调用返回值方法? - How to invoke a return value method with final class object as paremeter in Android ? 有没有办法检查一个类是否有一个方法,然后在已经存在的对象实例上调用它? - Is there a way to check if a class has a method and then invoke it on an already existing instance of the object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM