简体   繁体   English

私有类从其外部类调用方法

[英]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. 好的,所以我有一个用于“高级数据结构”的类(在本例中为一棵树),所以我将Iterator隐含为其中的私有类。 So the iterator needs to implement a remove function to remove the last retuirned element. 因此,迭代器需要实现remove函数以删除最后一个已恢复元素。

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. 现在,我的ADT已经实现了remove函数,在这种情况下,通过为迭代器实现不同的remove函数,几乎没有收益(想想,我认为没有)。

so how do I go about calling the remove from my ADT 所以我该如何从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. 因此,仅调用remove(FooInstance)将不起作用(对吗?),而this.remove(FooInstance)是同一回事。

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) (并且不能更改ADT的remove函数的名称,因为AD T必须满足Interace的要求,我可以自由更改)

I could make both of them call a removeHelper functon, I guess... 我想我可以将它们都称为removeHelper函数。

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: 要获取内部类“附加”的外部类的引用,请在您的情况下使用ClassName.this:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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