简体   繁体   中英

private class calling a method from its outer class

Ok, so I have a class for a "Advanced Data Structure" (in this case a kinda tree) SO I implimented a Iterator as a private class with in it. So the iterator needs to implement a remove function to remove the last retuirned element.

now my ADT already impliments a remove function, and in this case there is very little (thinking about it, i think nothing) to be gain by implimenting a different remove function for the iterator.

so how do I go about calling the remove from my ADT

sketch of my struture:

public class ADT {
...
   private class ADT_Iterator impliments  java.util.Itorator{
      ...
      public void remove(){
          //where I want to call the ADT's remove function from
      }
...

    public void remove( Object paramFoo )
    {
     ...
    }

    ...     

}

So just calling remove(FooInstance) won't work (will it?) and this.remove(FooInstance) is the same thing.

what do i call? (and changign the name of the ADT's remove function is not an option, as that AD T has to meet an Interace wich I am note at liberty to change)

I could make both of them call a removeHelper functon, I guess...

ADT.this.remove(object)

(尽管在这种情况下仅调用remove(object)将起作用,因为它的签名与迭代器中的remove()方法的签名不同。)

To get the reference for the outer class which the inner class is "attached" use ClassName.this, in your case:

   private class ADT_Iterator impliments  java.util.Itorator{
      ...
      public void remove(){
          ADT.this.remove(obj)
      }

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