一直没有想明白这个怎么实现的。本来已经实现了接口的回调,但是在接口回调里面还有一个参数
,那这个接口参数怎么和类的变量结合在一起呢?
比如:
interface A {
public void aa(B b);
}
class C {
A a = null;
B b = null;
public void C(){
b = new B();
}
public void setListener( A a){
this.a = a;
}
public void printThis(){
a.aa(b);
}
}
class B {
public void B(){
System.out.println("B");
}
public void printThis(){
System.out.println("I'm B");
}
}
public class Test(){
public static void main(String[] args){
C c = new C();
c.setListener(new A(){
public void aa(B b){
b.printThis();//问题来了,这个地方的b是刚刚申明的,而且还没有实例化,如何才能将值付给它呢?
}
});
c.printThsi();
}
}
我在使用高德地图的时候,就有这样的例子
AMapLocationListener mAMapLocationListener = new AMapLocationListener(){
@Override
public void onLocationChanged(AMapLocation amapLocation) {
String str = "";
TextView p1 = (TextView)findViewById(R.id.textView1);
if (amapLocation != null) {
Log.v("test info","amapLocation 不是空");
if (amapLocation.getErrorCode() == 0) {
Log.v("test info","amapLocation 成功");
//定位成功回调信息,设置相关消息
amapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表
amapLocation.getLatitude();//获取纬度
amapLocation.getLongitude();//获取经度
amapLocation.getAccuracy();//获取精度信息
}
}
这里的amapLocation是在什么时候获得的值呢?