私有实例变量/类定义

时间:2013-12-19 15:28:26

标签: java class private

我正在尝试解决这个问题:

一本书,作为标题,作者和出版年份。包括获取和设置私有实例变量的方法以及用于显示对象的toString方法。还要创建一个方法moreRecent,它将两本书作为输入参数,并返回最近发布的一本书。为moreRecent创建3个JUnit测试。

但是,我不知道如何访问其他私有实例变量author和yearofPub。我每个类只能做一个构造函数吗?那么如何获得其他2个变量呢?到目前为止这是我的代码: 谢谢!

class Book {
private String title;
private String author;
private String yearofPub;

Book(String input) {
    title = input;
}


String moreRecent(int Book1, int Book2) {
    String moreRecent;
    if(Book1 > Book2) {
        moreRecent = "Book 1";
    }
    if (Book2 > Book1) {
        moreRecent = "Book 2";
    }
    else 
        moreRecent = "Both";
    return moreRecent;

}

}

public class Chpt2930BookTitleAuthorYear {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter title of Book: ");
    String nextLine = scan.next();
    Book titleofBook = new Book(nextLine);

}
}

EDIT --------------------------

好的,谢谢你的回复! 我进一步改进了我的代码,但仍然无法得到正确的结果。我特别难以使用moreRecent方法。如何为这些书设置yearofPub?这是我的代码:

class Book {

private String title;
private String author;
private String yearofPub;

Book(String input) {
    title = input.toString();
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getAuthor() {
    return author;
}

public void setAuthor(String author) {
    this.author = author;
}

public String getYearofPub() {
    return yearofPub;
}

public void setYearofPub(String yearofPub) {
    this.yearofPub = yearofPub;
}

public String toString() {
    System.out.println("Book{" + "Title=" + title + " Author=" + author + " Year of
      Publication=" + yearofPub + "}");
    return " ";
}
//confused on this part :(
String moreRecent(Book Book1, Book Book2) {
    if (Book1.getYearofPub>Book.getYearofPub) { //Is this correct? I'm not sure :/

    }
}
}

public class Chpt2930BookTitleAuthorYear {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter title of Book: ");
    String nextLineTitle = scan.nextLine();
    Book titleofBook = new Book(nextLineTitle);


    System.out.println("Enter author of the Book: ");
    String nextLineAuthor = scan.nextLine();
    titleofBook.setAuthor(nextLineAuthor);


    System.out.println("Enter the date of publication of the Book: ");
    String nextLineDate = scan.nextLine();
    titleofBook.setYearofPub(nextLineDate);

    titleofBook.toString();

}
}

谢谢!

编辑2 -----------------

Nvm'd我发现了问题:) 感谢您提供的所有帮助

2 个答案:

答案 0 :(得分:2)

这样做:

class Book {

    private String title;
    private String author;
    private String yearofPub;

    Book(String input) {
        title = input;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getYearofPub() {
        return yearofPub;
    }

    public void setYearofPub(String yearofPub) {
        this.yearofPub = yearofPub;
    }



}

在您的主要方法中,您可以:

Book titleofBook = new Book(nextLine);
titleofBook.setAuthor(...) 

答案 1 :(得分:1)

为变量提供所谓的访问器。例如:

public String getAuthor() { 
   return this.author; 
}

public void setAuthor(String author) { 
   this.author = author; 
}

同样适用于yeaofPub字段。