java - 你能把数组放到Java中的业务方法中吗
问题描述
我的主要方法必须有一个包含 2 个团队名称的数组,我想知道是否有办法在业务类中使用该数组,然后将其带到测试类来设置名称。
//instance variables
String[] names = new String[2];
int[] score;
//no-arg constructor
public ReHW3Biz() {
names[0] = "Null";
names[1] = "Null";
score[] = 0;
}
//getters and setters
public String[] getName(String[] names)
{
this.names = names;
}
我收到的错误消息是:
此方法必须返回 String[] 类型的结果
解决方案
getName 方法的返回类型是 String[]。所以你需要返回一个数组。
public String[] getName(String[] names)
{
this.names = names;
return this.names;
}
但最好使用 setter 方法设置名称值和使用 getter 方法获取值。
public String[] getName() {
return this.names;
}
public void setName(String[] names) {
this.names = names;
}
推荐阅读
- python - 如何计算每条记录中的单词并在最佳时间将其存储在单词和键级别?
- java - 示例 keycloak spring-boot 应用程序无法找到 bean KeycloakSpringBootConfigResolver
- r - 如何在 GGparcoord R 的平行坐标图中添加刻度?
- perl - 在 perl 中添加到哈希时遇到问题
- go - 使用 Go Olivere/elastic 过滤 value1 == value2 的 Elasticsearch 查询
- qt - QDateTime 自定义格式转义符号
- django - 运行“迁移”不会生成迁移文件夹
- java - 在 OpenAPI 中启用异步服务器 API,按需为 Jersey 生成代码
- c - 如何使我的代码按升序排序输入?
- pyspark - 如何从字符串中生成日期时间