java - 从Java中的匿名对象访问非静态对象

标签 java gwt static anonymous-class

使用 Google Web Toolkit,我编写了一个基于模型- View - Controller 概念的应用程序。现在我的ClientController类有两个 ClientModel 类型的对象和ClientView它们被声明为私有(private)和非静态。对于异步调用的对象,我编写了一些确实有效的远程过程调用,但是附加了类型 AsyncCallback<T> 的匿名对象。这让我遇到了问题,因为我无法访问这两个 ClientModelClientView对象而不写一些令人讨厌的final我的函数中的变量如下所示:

package com.foo.bar;

/**
 * Represents the main handler holding delegates and controlling the contents of
 * the {@link ClientModel} and pushes notifications and message to both the
 * {@link ClientModel} and the {@link ClientView}.
 */
public class ClientController {
    /**
     * Exposes asynchronous functions to call the server component via RPC.
     */
    public static final MyServiceAsync mySvc = GWT.create(myService.class);

    /**
     * Represents the model associated with the {@link ClientController} object.
     */
    private ClientModel theModel = null;
    /**
     * Represents the view associated with the {@link ClientController} object.
     */
    private ClientView theView = null;

    /**
     * Creates a new {@link ClientController} object and instantiates both the
     * {@link ClientModel} and the {@link ClientView}.
     */
    public ClientController() {
        this.theModel = new ClientModel();
        this.theView = new ClientView(this.theModel);
    }

    /* some more code */

    /**
     * Tries to login the specified user and updates the {@link ClientView}
     * object to either an error message or the main interface.
     * 
     * @param user
     *            {@link User} object representing the user to login
     */
    public void loginUser(final User user) {
        ///////////////////////////////////////////////
        // THIS IS UGLY AND I DON'T KNOW HOW TO FIX THIS
        ///////////////////////////////////////////////
        final ClientModel currentModel = this.theModel;

        // Execute the login protocol
        ClientController.mySvc.login(user, new AsyncCallback<Boolean>() {
            /**
             * The request was successfully executed. Returns a boolean value
             * indicating whether the user was logged in.
             * 
             * @param result
             *            true, if the user was logged in; otherwise, false.
             */
            @Override
            public void onSuccess(Boolean result) {
                // The user was successfully logged in and we must both store
                // him in the model and then update the view.
                if (result) {
                    // TODO: Update the view to show the chat itself and save
                    // the current User to the ClientModel.
                    System.out.println("The User " + user.getUsername()
                            + " is now logged in!");
                    // Anonymous object can not access the theModel and theView
                    // objects of ClientController directly ...
                    // drunk, fix later!
                    // UGLY FIX FOR NOW
                    currentModel.setCurrentUser(user);
                } else {
                    // TODO: Unhide the error label of the login form and output
                    // some nice error message.
                    System.out.println("Login failed for User "
                            + user.getUsername() + "!");
                }
            }

            /**
             * The request provoked an error.
             */
            @Override
            public void onFailure(Throwable up) {
                try {
                    throw up; // Ha ha
                } catch (Throwable e) {
                    // Who cares?
                }
            }
        });
    }
}

现在我正在使用 loginUser 中指向内部模型的最终指针。它被高亮显示两次。谁能解释一下是否有更好的解决方案而不移动theModeltheView静态成员?在您访问任一组件的每个函数中编写这样的“最终包装器”是很烦人的......

最佳答案

当您创建非静态内部类的实例或匿名类的实例时,该实例会隐式绑定(bind)到外部类的实例创建它的类。您可以使用 OuterClassName.this.member 从内部类中访问外部类的成员。

在您的情况下:ClientController.this.theModel

关于java - 从Java中的匿名对象访问非静态对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7283476/

相关文章:

java - 我的子类如何正确继承静态变量

java - fragment 中的 onRefresh swiperefreshlayout 调用我的主要 Activity 类方法来替换我的 fragment

c - 我在哪里可以找到在我的 C 程序的 .data 部分中创建静态变量的程序集?

java - Jersey 2.11 和 ResourceMethodInitationHandlerProvider : MessageBodyWriter not found

java - 尝试使用两个 if 语句打印树的顶 View

java - 字符串格式不起作用

java - 使用 Spring MVC 映射资源

javascript - 将 JavaScript 值传递到 Java GWT

java - 检测 GWT 中的离线和在线状态

gwt - GWT 中的 System 类型的 nanoTime() 方法未定义