【发布时间】:2013-04-21 07:47:02
【问题描述】:
类A 实现接口I 需要方法doAction()。如果我从类A of 类B 调用一个方法,并将“this”(类A)传递给该方法,我怎么能调用一个存在于类中的方法A 来自 B 类中的方法?例如:
class A implements I {
public void start() {
B.myMethod(this);
}
@Override
public void doAction() {
// Do stuff...
}
}
Class B {
public void myMehtod(Class theClass) { //How would I accept 'this', and...
theClass.doAction(); //How would I call the method?
}
}
我这样做是为了自定义库,但不知道扩展 I 的类的确切名称。
【问题讨论】:
-
您在“myMehtod”中有错字
-
根据您的最终目标,您可能会发现阅读Command Pattern 很有帮助。
标签: java methods interface this implementation