在构造函数之间以某种方式设置值,然后在Main中调用所述值,它们变为null。代码只是让用户输入书籍作者,标题和流派,然后显示这些值。
package book;
import java.util.Scanner;
public class Book {
Scanner input = new Scanner(System.in);
public String author;
public String title;
public String genre;
public Book(String author, String title, String genre) {
String bookAuthor = author;
String bookTitle = title;
String bookGenre = genre;
System.out.println(bookAuthor + bookTitle + bookGenre);
System.out.println(author + title + genre);
}
public Book() {
String bookAuthor;
String bookTitle;
String bookGenre;
}
public void setTitle() {
System.out.print("What is the title of the book? ");
title = input.nextLine();
}
public void setAuthor() {
System.out.print("Who wrote this book? ");
author = input.nextLine();
}
public void setGenre() {
System.out.print("What genre category does this book fall under? ");
genre = input.nextLine();
}
public void getAuthor() {
System.out.printf("\nAuthor: %s", author);
}
public void getTitle() {
System.out.printf("\nTitle: %s", title);
}
public void getGenre() {
System.out.printf("\nGenre: %s", genre);
}
public static void main(String[] args) { //starting point of any Java program
Book book = new Book(); //create new instance of Book() so that main can modify it
book.setTitle();
book.setAuthor();
book.setGenre();
Book myBook = new Book(book.author, book.title, book.genre);
System.out.println(myBook.author + myBook.title + myBook.genre);
myBook.getAuthor();
myBook.getTitle();
myBook.getGenre();
}
}
答案 0 :(得分:4)
你的问题在这里:
public Book(String author, String title, String genre){
String bookAuthor = author;
String bookTitle = title;
String bookGenre = genre;
System.out.println(bookAuthor + bookTitle + bookGenre);
System.out.println(author + title + genre);
}
您正在书籍构造函数中声明本地/堆栈变量并将输入参数分配给它们。然后它们在构造函数退出时消失,因为它们不再在范围内。我认为你的意思是这样做,并将构造函数参数分配给对象数据成员:
public Book(String authorIn, String titleIn, String genreIn){
author = authorIn;
title = titleIn;
genre = genreIn;
}
此外,那些无效的getter方法没有任何意义。
答案 1 :(得分:0)
正如OldProgrammer所说,你不需要隐藏变量,但更重要的是,要让用户交互超出Book类。它应该只封装Book信息,所有用户交互都应该去其他地方。此外,setter和getter应该是真正的setter和getter,即setter方法设置字段和getter方法返回。
例如getter / setter对,请考虑字符串字段foo:
private String foo;
// getter
public String getFoo() {
return foo;
}
// setter
public void setFoo(String foo) {
this.foo = foo;
}
例如,Book的简单版本可能如下所示:
public class SimpleBook {
private String author;
private String title;
public SimpleBook(String author, String title) {
this.author = author;
this.title = title;
}
// getter method returns the field's value
public String getAuthor() {
return author;
}
// setter method sets the field's value
public void setAuthor(String author) {
this.author = author;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
// so the book can be displayed cleanly
@Override
public String toString() {
return "Book [Author: " + author + ", title: " + title + "]";
}
}
然后用户交互可以进入测试类:
import java.util.Scanner;
public class TestSimpleBook {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an Author: ");
String author = scanner.nextLine();
System.out.print("Enter an Title: ");
String title = scanner.nextLine();
SimpleBook book = new SimpleBook(author, title);
System.out.println("Your Book: " + book);
scanner.close();
}
}