简体   繁体   中英

Passing “this” from Java Interface to another Class

Giving the following class of my android Project :

Preview extends SurfaceView implements SurfaceHolder.Callback

and

A extends Doc

I don't really know how to ask and I know that this is not really good but I want that from Preview, I call an abstract method of Doc. In this Doc's method, I have to call a method of the previous Object of Preview.

This is an example :

From Preview.java :

   Doc _doc = new A();
   private void myMethod() {
       this._doc.process(this)
   }

From A.java :

@Override
public void process(Preview p) {
    p.processA();
}

The problem is that I got an error :

The method process(Preview) in the type Doc is not applicable for the arguments (new Camera.PreviewCallback(){})

However, I can't change this judging by the fact that I want to call the method from Preview. I tried many thing such as cast etc. None of them works.

Thanks for your help !

PS : I am on Eclipse under Windows.

假设您正在从匿名内部类(我认为是Camera.PreviewCallback类型,因此是错误消息)中调用A.process(this) ,则必须编写A.process(Preview.this) ,因为独立的this引用了内部类而不是Preview实例。

The method process(Preview) in the type Doc is not applicable for the arguments (new Camera.PreviewCallback(){})

It simply means you are passing the object of Camera.PreviewCallback but in your method public void process(Preview p) You want an object of Preview .

If you have written this code in side an anonymous class , then this won't point to the Preview class. It will point to the object of inner anonymous class.

Thus you need to write A.process(Preview.this)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM