gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:27:44 24 4
gpt4 key购买 nike

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

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

但是,我不知道如何访问其他私有(private)实例变量 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);

}
}

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

好的,谢谢大家的回复!我进一步改进了我的代码,但仍然无法获得正确的结果。我特别难以使用 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 我发现了问题 :)感谢所有的帮助

最佳答案

这样做:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com