在示例中使用setter方法和简单地声明变量有什么区别?

在示例中使用setter方法和简单地声明变量有什么区别?,setter,Setter,我只是想知道,使用setter方法,setLocationCells,和简单地用值{2,3,4}声明一个名为locationCells的int数组有什么区别 下面是使用setter方法的代码(第一个类是主类): 二等舱: int[] locationCells; int numOfHits = 0; public void setLocationCells(int[] locs) { locationCells = locs; } public String checkYo

我只是想知道,使用setter方法,
setLocationCells
,和简单地用值{2,3,4}声明一个名为
locationCells
的int数组有什么区别

下面是使用setter方法的代码(第一个类是主类):

二等舱:

int[] locationCells;
int numOfHits = 0;

public void setLocationCells(int[] locs) {
    locationCells = locs;

}   


public String checkYourself(String stringGuess) {
    int guess = Integer.parseInt(stringGuess); 


    String result = "miss";
    for (int cell : locationCells) {
        if (guess == cell) {
            result = "hit";
            numOfHits++;
            break;
        }
    }

        if(numOfHits == locationCells.length) {
            result = "kill";
        }



    System.out.println(result);
    return result;
}
int[] locationCells = {2,3,4};
int numOfHits;

public String checkYourself(String stringGuess) {
    int guess = Integer.parseInt(stringGuess); 


    String result = "miss";
    for (int cell : locationCells) {
        if (guess == cell) {
            result = "hit";
            numOfHits++;
            break;
        }
    }

        if(numOfHits == locationCells.length) {
            result = "kill";
        }



    System.out.println(result);
    return result;
}
现在,如果没有setter方法:

public static void main(String[] args) {

    SimpleDotCom dot = new SimpleDotCom();
    String userGuess = "2";
    String result = dot.checkYourself(userGuess);

}
二等舱:

int[] locationCells;
int numOfHits = 0;

public void setLocationCells(int[] locs) {
    locationCells = locs;

}   


public String checkYourself(String stringGuess) {
    int guess = Integer.parseInt(stringGuess); 


    String result = "miss";
    for (int cell : locationCells) {
        if (guess == cell) {
            result = "hit";
            numOfHits++;
            break;
        }
    }

        if(numOfHits == locationCells.length) {
            result = "kill";
        }



    System.out.println(result);
    return result;
}
int[] locationCells = {2,3,4};
int numOfHits;

public String checkYourself(String stringGuess) {
    int guess = Integer.parseInt(stringGuess); 


    String result = "miss";
    for (int cell : locationCells) {
        if (guess == cell) {
            result = "hit";
            numOfHits++;
            break;
        }
    }

        if(numOfHits == locationCells.length) {
            result = "kill";
        }



    System.out.println(result);
    return result;
}

非常感谢您的帮助,非常感谢

因为有很多方法可以分配对象成员,所以使用getter和setter有助于简化类内部和使用它的内部之间的契约。这是通过公开接口并通常将类成员声明为私有来实现的

如果以后更改这些成员的分配方式,提供数据验证和转换,则不必担心更改分配它们的每个其他位置,只需更新setter或getter的内部实现即可。您的示例看起来很琐碎、正确,但setter和getter有助于随着项目的增长降低复杂性。当您使用接口时,它提供了一个更安全的约定,因为您可以处理除数据类型之外的各种微妙条件,例如值为0或成员引用为null时的状态


这里的设计原则被称为。

读了你的评论后,我记得这实际上在教科书的某个地方提到过,但我想我只是在这个话题上犯了错误。教科书还指出,在大型项目中,getter和setter将非常有用(例如,如您所述,无需每隔一个地方进行更改)。非常感谢你!