gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:37:44 25 4
gpt4 key购买 nike

使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com