首页 > 解决方案 > java中的setter和getter

问题描述

我有两个文件,一个是驱动程序,我遇到了 setter 的问题。它看起来确实设置了值。

public class Movie {
private String name;
private int minutes;
protected int tomatoScore;

public Movie(String name, int minutes, int tomatoScore)
{
    this.name=name;
    this.minutes=minutes;
    this.tomatoScore=tomatoScore;
}

public String getName() {return name;}
public void setName(String name) {this.name=name;}
public int getMinutes() {return minutes;}
public boolean setMinutes(int minutes) {return minutes>=0;}
public int getTomatoScore() {return tomatoScore;};
public boolean setTomatoScore(int tomatoScore) {return tomatoScore>=0 &&tomatoScore<=100;};
public boolean isFresh() {return tomatoScore>=60;};

public void display()
{
    //this.name = name;
    //this.minutes = minutes;
    //this.tomatoScore =tomatoScore;

    System.out.println("Movie: "+ getName());
    System.out.println("Length: "+ getMinutes() +"min.");

    if(tomatoScore>=60)
    {
        System.out.println("TomatoScore: Fresh");
    }
    else 
    {
        System.out.println("TomatoScore: Rotten");
    }


}

}

如果您注意到设置器确实完成了应该做的工作,那么下面是驱动程序文件我相信问题是电影类,如果您运行驱动程序来测试程序,您会看到是否将值设置为负不能正常工作。( setMinutes 和 setTomatoScore 是错误的。他们根本没有设置类字段)

公共类 MovieDriver {

public static void main (String [] args){

    Movie[] myCollection = new Movie[5];
    myCollection[0] = new Movie("Batman The Dark Knight", 152, 94);
    myCollection[1] = new Movie("Guardians of the Galaxy", 125, 91);
    myCollection[2] = new Movie("The GodFather", 178, 98);
    myCollection[3] = new Movie("Suicide Squad", 137, 27);
    myCollection[4] = new Movie("Get out", 104, 99);


    //TODO
    //Initialize the variable below and add it to myCollection at index 4.
    //You can pick any movie you wish.
    Movie yourMovie;


    System.out.println("Here are all the movies in my collection of movies.\n");
    for(int i = 0; i < myCollection.length; i++) {
        if(myCollection[i] != null)
            myCollection[i].display();
    }

    System.out.println("_______________________________________________");


    System.out.println("\nHere are the Fresh movies.");

    for(int i = 0; i < myCollection.length; i++) {
        if(myCollection[i] != null && myCollection[i].isFresh()) {
            System.out.println(myCollection[i].getName() + " is fresh.");
        }
    }

    System.out.println();
    System.out.println("Here are the Rotten movies.");

    for(Movie movieTmp: myCollection){
        if (movieTmp != null && !movieTmp.isFresh())
            System.out.println(movieTmp.getName() + " is rotten.");
    }

    System.out.println("_______________________________________________\n");

    Movie harryPotter = new Movie("Harry Potter and the Prisoner of Azkaban", 144, 91);
    System.out.println("The movie " + harryPotter.getName() + " was created.\n");

    System.out.println("Is " + harryPotter.getName() + " a long movie?");

    if(harryPotter.getMinutes() > 120) {
        System.out.println("Yes, it is a bit long.\n");
    } else {
        System.out.println("Nope, that isn't too bad.\n");
    }

    System.out.println("Can I set the minutes of " + harryPotter.getName() + " to a negative number?");
    harryPotter.setMinutes(-5);

    if(harryPotter.getMinutes() == -5) {
        System.out.println("It worked. The runtime is -5 minutes.\n");
    } else {
        System.out.println("It did NOT work.  Negative runtimes are not allowed.\n");
    }

    System.out.println("Can I set tomato score of " + harryPotter.getName() + " to a negative number?");
    harryPotter.setTomatoScore(-100);

    if(harryPotter.getTomatoScore() == -100) {
        System.out.println("It worked. The score is -100.  This movie is terrible according to the site.\n");
    } else {
        System.out.println("It did NOT work.  Negative scores are not allowed.\n");
    }

    System.out.println("Can I set tomato score of " + harryPotter.getName() + " to a number greater than 100?");
    harryPotter.setTomatoScore(101);

    if(harryPotter.getTomatoScore() == 101) {
        System.out.println("It worked. The score is 101.  Best Harry Potter movie ever!\n");
    } else {
        System.out.println("It did NOT work.  Still the best Harry Potter movie out all the movies though.\n");
    }


}

}

标签: javagetter-settersettergetter

解决方案


你的setMinutessetTomatoScore方法没有设置任何东西,它们只是返回一个布尔值。例如,我假设您忘记添加this.tomatoScore = tomatoScore


推荐阅读