首页 > 解决方案 > 你能把数组放到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[] 类型的结果

标签: javaarrays

解决方案


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;
}

推荐阅读