java - 私有(private)实例变量/类定义

标签 java class private

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

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

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

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

}
}

编辑--------------------------

好的,谢谢大家的回复! 我进一步改进了我的代码,但仍然无法得到正确的结果。我对 moreRecent 方法尤其感到困难。如何设置这些书的年份?这是我的代码:

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 我发现了问题:) 不过还是感谢您的帮助

最佳答案

这样做:

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(...) 

关于java - 私有(private)实例变量/类定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20685489/

相关文章:

java - java中如何创建String对象(通过以下代码)

java - 用于接收短信的应用程序第一次运行时工作正常,第二次运行时所有消息均不显示

java - 使用流程构建器来启动类(class)?

C++,从函数调用分配给类实例?

jquery - 添加此 jQuery 后无法取消选中复选框

.net - 如何对私有(private)方法进行单元测试?

java - 在java中,当使用正则表达式查找模式时,如何获得嵌套结果?

java - 在 Android 中实现 SOAP API

Python OpenSSL 生成公钥和私钥对

c++ - 访问者接口(interface)的私有(private)继承如何允许可访问对象访问访问私有(private)访问实现?