gpt4 book ai didi

Java 类常量

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

我想为我也创建了一个实例的类定义一些公共(public)类常量类变量。

在我的 Response 类中,我定义了一些我想在整个应用程序中用作常量的 responseCodes。

请不要担心这段代码的意义,这只是片段的复制和粘贴,以展示我如何尝试使用类常量。

我的 IntelliJ 根本没有提示这种语法,但是当我尝试构建代码时,我得到了

Filter.java:[84,36] cannot find symbol
symbol : variable BR_PARTIALLY_OK

我的带有常量的示例类

package xx.xx.xxxxx.connector;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Response {
public static final int BR_OK = 200;
public static final int BR_PARTIALLY_OK = 250;
public static final int BR_NOT_AUTHORIZED = 401;
public static final int BR_REQUEST_TIMEOUT = 408;
public static final int BR_NOT_IMPLEMENTED = 501;
public static final int BR_SERVICE_UNAVAILABLE = 503;

private Map<String, List<String>> headers = new HashMap<String, List<String>>();
private String body = null;

public void addHeader(String key, List<String> value) {
this.headers.put(key, value);
}

public String getBody() {
return body;
}

public void setBody(String body) {
this.body = body;
}

public Map<String, List<String>> getHeaders() {
return headers;
}
}

在另一个类中使用的示例

package xx.xx.xxxxx.filter; // other package, other maven artifact but depends response package
import xx.xx.xxxxx.connector.Response;

public class Filter {
public int doFilter(Service service) {
Response myResponse = service.post(..);
return Response.BR_PARTIALLY_OK;
}
}

我也试过

import static xxx.xxx.Response.*

import static xxx.xxx.Response.BR_PARTIALLY_OK;

和使用

return BR_PARTIALLY_OK

导入时已经出现相同的“找不到符号”错误

我看过接口(interface)和 const 类的例子,但我想知道为什么这个例子不起作用

编辑:也许问题在于该类是在依赖工件中定义的?

pom.xml

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>xxx.xxxxx.xxx</groupId>
<artifactId>connector</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>used the Response.BR_PARTIALLY_OK constant here</groupId>
<artifactId>filter</artifactId>
<version>${connector.filter.version}</version>

<dependencies>
<dependency>
<groupId>defined the Response class in this artifact</groupId>
<artifactId>component</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>info.magnolia</groupId>
<artifactId>magnolia-core</artifactId>
<version>4.5.4</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>net.sourceforge.openutils</groupId>
<artifactId>openutils-log4j</artifactId>
<version>2.0.5</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>

<!-- Testing Dependencies -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<repositories>
<repository>
<id>magnolia.public</id>
<url>http://nexus.magnolia-cms.com/content/groups/public</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</project>

最佳答案

我发现错误信息有点不对。

“变量 BR_PARTIALLY_OK”

变量?这将使我假设代码不包含“Response.BR_PARTIALLY_OK”,而只包含“BR_PARTIALLY_OK”。

关于Java 类常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13358579/

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