gpt4 book ai didi

java - 如何在匿名类中访问变量?

转载 作者:行者123 更新时间:2023-11-30 08:09:27 24 4
gpt4 key购买 nike

我有以下代码:

    class A  extends HttpServlet{
protected void doGet(HttpServletRequest request, HttpServletRespons
response) throws ServletException, IOExceptio
{
String str= "String In doGet()";
JsonBatchCallback<Users> callback = new
JsonBatchCallback<Users>(){
String inThisClass = str; // Showing me error
}
}
}

str 不可访问。我如何访问“str”

我的实际代码如下:

public class SyncTask extends HttpServlet {

private static final long serialVersionUID = 1L;

final Logger logger = Logger.getLogger(this.getClass().getName());

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String domain = request.getParameter("DomainName");
String reqAdminEmail = request.getParameter("AdminEmail");
String searchRegex = request.getParameter("searchRegex");

Boolean manualSync = false;

if(reqAdminEmail != null){
manualSync = true;
}

String adminEmail = "";

try{

Date startTime = new Date();

Manager mangerObj = new Manager("MASTER");

final String SERVICE_ACCOUNT_EMAIL = Constant.getServiceAccountEmail();

final String SERVICE_ACCOUNT_PKCS12_FILE_PATH = Constant.getServiceAccountPkcs12FilePath();


Collection<String> SCOPES = new ArrayList<String>();
SCOPES.add("https://www.googleapis.com/auth/admin.directory.user");
SCOPES.add("https://www.googleapis.com/auth/admin.directory.orgunit");


String nextToken = null;
int noOfUsers = 0;

mangerObj = new Manager(domain);

Configuration config = mangerObj.getConfiguration();
if(config==null)
return;
else
adminEmail = config.getAdminEmail();


HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();

GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes(SCOPES)
.setServiceAccountUser(adminEmail)
.setServiceAccountPrivateKeyFromP12File(
new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH))
.build();

Directory directoryService = new Directory.Builder(httpTransport, jsonFactory, credential).setApplicationName("Directory ").build();
BatchRequest batch = directoryService.batch();

do{

List list = directoryService.users().list();
list.setCustomer("my_customer");
list.setMaxResults(500);
list.setPageToken(nextToken);
list.setFields("nextPageToken,users(relations,orgUnitPath,primaryEmail,name)");
if(searchRegex != null ){
if(searchRegex.isEmpty() == false){
list.setQuery("email:"+searchRegex+"*");
}
}

JsonBatchCallback<Users> callback = new JsonBatchCallback<Users>() {

@Override
public void onSuccess(Users users, HttpHeaders responseHeaders) {
nextToken = users.getNextPageToken(); // i'm not able to access nextToken
}

public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) {
System.out.println("Error Message: " + e.getMessage());
}
};

list.queue(batch, callback);

}while(nextToken != null);

try {
batch.execute();
} catch(Exception ex){
ErrorHandler.errorHandler(this.getClass().getName(), ex);
}

}catch(Exception ex){
ErrorHandler.errorHandler(this.getClass().getName(), ex);
}

}

我已经更新了我的代码,但实际上我遇到了错误。我想访问 nextToken 进入匿名类,但我无法访问。

错误如下:

nextToken cannot be resolved to a variable

最佳答案

您需要将 str 变量设为 final

事实上,inThisClass 变量是多余的,至少在您到目前为止发布的内容中是这样。

但是您的实际代码显示的错误消息与示例代码不同。无法编写您的实际代码,因为您没有对匿名类的封闭范围中的局部变量进行写访问的权限。您将不得不考虑其他东西,例如 final StringBuilder。

关于java - 如何在匿名类中访问变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30610895/

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