[英]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.