Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将外部anon类ref传递给内部anon类中的方法_Java_Oop_Scope_Anonymous Class_Anonymous Inner Class - Fatal编程技术网

Java 将外部anon类ref传递给内部anon类中的方法

Java 将外部anon类ref传递给内部anon类中的方法,java,oop,scope,anonymous-class,anonymous-inner-class,Java,Oop,Scope,Anonymous Class,Anonymous Inner Class,在Java中,如何将外部anon类ref传递给内部anon类中的方法 我有一个对服务器进行异步调用的方法-sendCall(一些参数,回调)。回调由匿名类(我们将其命名为OuterAnon)表示,并包含一个失败案例的方法。在该方法中,会创建一个消息框,并在每次按下OK按钮时调用sendCall()。因此,我需要再次将外层传递给该方法 下面是一段代码来演示我的意思: private void sendCall(MyData data, OuterAnon<Boolean> callba

在Java中,如何将外部anon类ref传递给内部anon类中的方法

我有一个对服务器进行异步调用的方法-
sendCall(一些参数,回调)
。回调由匿名类(我们将其命名为
OuterAnon
)表示,并包含一个失败案例的方法。在该方法中,会创建一个消息框,并在每次按下OK按钮时调用
sendCall()
。因此,我需要再次将
外层
传递给该方法

下面是一段代码来演示我的意思:

private void sendCall(MyData data, OuterAnon<Boolean> callback){/*...*/}

private void myCall(final MyData data) {
        sendCall(data, new OuterAnon<Boolean>() {
            public void onFailure(Throwable throwable) {
                    final OuterAnon<Boolean> callback = this; //how to avoid this?
                    MessageBox.show(throwable.getMessage(), new MessageListener() {
                        public void process(MessageBox.OnClick action) {
                            if (action == MessageBox.OnClick.OK) {
                                sendCall(new MyData("resend?"), callback);
                            }
                        }
                    });
                }
            }
        });
    }
但我希望避免创建ref并传递回调,如:

sendCall(new MyData("resend?"), this); //at the moment we point to MessageListener instead of OuterAnon.

在Java中有什么方法可以做到这一点吗?

我们很难解决这个问题,因为您只显示了不完整的代码和未提供的类,所以我不知道这个示例在语法上是否正确。也就是说,这样的重构可能适合您的需要:

  private void myCall(final MyData data)
  {
    sendCall(data, new OuterAnon<Boolean>()
    {
      public void onFailure(Throwable throwable)
      {
        showErrorMessage(throwable);
      }
    });
  }

  private void showErrorMessage(Throwable throwable)
  {
    MessageBox.show(throwable.getMessage(), new MessageListener()
    {
      public void process(MessageBox.OnClick action)
      {
        if (action == MessageBox.OnClick.OK)
        {
          sendCall(new MyData("resend?"));
        }
      }
    });
  }

  private void sendCall(MyData data)
  {
    sendCall(data, this);
  }
private void myCall(最终MyData数据)
{
sendCall(数据,新的外层空间()
{
失败时的公共无效(可丢弃)
{
发送信息(可丢弃);
}
});
}
私人信息(可丢弃可丢弃)
{
show(throwable.getMessage(),newmessagelistener())
{
公共作废流程(MessageBox.OnClick操作)
{
if(action==MessageBox.OnClick.OK)
{
sendCall(新建MyData(“重新发送”);
}
}
});
}
私有void sendCall(MyData数据)
{
sendCall(数据,此);
}
一般来说,我认为将代码从一个非内部类中抽象出来,并放到封闭类上它们自己的方法中通常是个好主意。它现在是可测试的、可重用的,并且可读性更高。

如果您确实需要以显示代码的方式在内部类中指定
onFailure
,并且如果您需要对
回调使用该特定引用,并且需要以这种方式编码

让我们回答这个问题:不

在我的尝试中,我已经实现了3种方法来访问anon-inner-most实例中的anon-inner-least实例,但是我认为没有一种方法能够满足您的期望

在这种情况下,anon-inner-most没有对anon-inner-least的引用:正如您所说,
this
现在指向anon-inner-least

此外,我尝试在搜索,但找不到问题的确切答案-如果有人在那里找到答案,请贡献

我的尝试:

import java.util.ArrayList;
import java.util.LinkedList;

public abstract class AnonTest {

    public static void main(String[] args) {
        new ArrayList<Object>() {

            private static final long serialVersionUID = -5986194903357006553L;

            {
                // initialize inner anon class
                add("1");
            }


            // Way 1
            private Object thisReference1 = this;

            // Way 2
            private Object getThisReference2() {
                return this;
            }

            @Override
            public boolean equals(Object obj) {
                // Way 3
                final Object thisReference3 = this;
                new LinkedList<Object>() {

                    private static final long serialVersionUID = 900418265794508265L;

                    {
                        // initialize inner inner anon class
                        add("2");
                    }

                    @Override
                    public boolean equals(Object innerObj) {
                        // achieving the instance
                        System.out.println(thisReference1);
                        System.out.println(getThisReference2());
                        System.out.println(thisReference3);
                        System.out.println(this);

                        System.out.println();

                        // achieving the class
                        System.out.println(thisReference1.getClass());
                        System.out.println(getThisReference2().getClass());
                        System.out.println(thisReference3.getClass());
                        System.out.println(this.getClass());
                        System.out.println(this.getClass().getEnclosingClass());
                        return super.equals(innerObj);
                    }
                }.equals("");
                return super.equals(obj);
            }
        }.equals("");
    }
}
import java.util.ArrayList;
导入java.util.LinkedList;
公共抽象类测试{
公共静态void main(字符串[]args){
新ArrayList(){
私有静态最终长serialVersionUID=-5986194903357006553L;
{
//初始化内部anon类
添加(“1”);
}
//方式1
私有对象thisReference1=this;
//方式2
私有对象getThisReference2(){
归还这个;
}
@凌驾
公共布尔等于(对象obj){
//方式3
最终对象thisReference3=此;
新建LinkedList(){
私有静态最终长serialVersionUID=900418265794508265L;
{
//初始化内部anon类
添加(“2”);
}
@凌驾
公共布尔等于(对象innerObj){
//实现实例
System.out.println(本参考1);
System.out.println(getThisReference2());
System.out.println(此参考3);
System.out.println(本文件);
System.out.println();
//实现班级目标
System.out.println(thisReference1.getClass());
System.out.println(getThisReference2().getClass());
System.out.println(thisReference3.getClass());
System.out.println(this.getClass());
System.out.println(this.getClass().getEnclosuringClass());
返回super.equals(innerObj);
}
}.等于(“”);
返回super.equals(obj);
}
}.等于(“”);
}
}

因为这里的外部类也是匿名的,所以我认为没有办法做到这一点,如果外部类有一些名称,那么它可以引用。this@Krushna,尝试使用
OuterAnon访问它。这将导致错误:“OuterAnon不是封闭类”
  private void myCall(final MyData data)
  {
    sendCall(data, new OuterAnon<Boolean>()
    {
      public void onFailure(Throwable throwable)
      {
        showErrorMessage(throwable);
      }
    });
  }

  private void showErrorMessage(Throwable throwable)
  {
    MessageBox.show(throwable.getMessage(), new MessageListener()
    {
      public void process(MessageBox.OnClick action)
      {
        if (action == MessageBox.OnClick.OK)
        {
          sendCall(new MyData("resend?"));
        }
      }
    });
  }

  private void sendCall(MyData data)
  {
    sendCall(data, this);
  }
import java.util.ArrayList;
import java.util.LinkedList;

public abstract class AnonTest {

    public static void main(String[] args) {
        new ArrayList<Object>() {

            private static final long serialVersionUID = -5986194903357006553L;

            {
                // initialize inner anon class
                add("1");
            }


            // Way 1
            private Object thisReference1 = this;

            // Way 2
            private Object getThisReference2() {
                return this;
            }

            @Override
            public boolean equals(Object obj) {
                // Way 3
                final Object thisReference3 = this;
                new LinkedList<Object>() {

                    private static final long serialVersionUID = 900418265794508265L;

                    {
                        // initialize inner inner anon class
                        add("2");
                    }

                    @Override
                    public boolean equals(Object innerObj) {
                        // achieving the instance
                        System.out.println(thisReference1);
                        System.out.println(getThisReference2());
                        System.out.println(thisReference3);
                        System.out.println(this);

                        System.out.println();

                        // achieving the class
                        System.out.println(thisReference1.getClass());
                        System.out.println(getThisReference2().getClass());
                        System.out.println(thisReference3.getClass());
                        System.out.println(this.getClass());
                        System.out.println(this.getClass().getEnclosingClass());
                        return super.equals(innerObj);
                    }
                }.equals("");
                return super.equals(obj);
            }
        }.equals("");
    }
}