カスタム クラスで、パラメーターを介して外部から与えられた関数を実行しようとしています。
親クラスなどへの参照を保存する必要がなく、呼び出されるメソッド/関数のみを保存することが重要です。
メソッドをパラメーターとして送信しようとしましたが、機能しません。
class A
// var to store reference to external method
private Method myMethodReference; // (says error: never used)
// the setter to store the reference to the external method
public void setMethodReference(Method someMethod)
{
myMethodReference = someMethod;
}
public boolean someFunctionWithinMyClass()
{
// call the external method via my reference
myMethodReference(); // (says error: The method myMethodReference() is undefined)
}
また、外部クラスからメソッドへの参照を設定しようとすると、機能しません。
class B
public void someFunction() { Log.i("la", "la" };
instanceOfClassA.setMethodReference(someFunction); // (says error: variable someFunction not found)
したがって、Eclipse は someFunction がスコープ外の変数であると想定しているため、参照を渡しても機能しません。
これでよりよく説明できると思います!