需要明确的是,这是一项活跃的家庭作业。我只需要一些指导。
我正在用 Java 创建一个书店程序,在修订版 1 中,运行时只需返回一本特定书籍的信息。我必须使用两个类,并在main方法中调用辅助类中的信息来输出。
我认为我在 Book 类中正确创建了构造函数,但是 A) 定义这些变量和 B) 调用 Bookstore 类中的信息进行输出的最佳方法是什么?我只是需要一些指导,因为我有点陷入困境。 Eclipse 要求我将 int 和 double 变量重新定义为字符串,但它们将是数字...我的语法中是否有某些内容导致它这样做?
这是我到目前为止所拥有的:
import java.util.Scanner; // Import Scanner
/** Main Class */
public class Bookstore {
/** Secondary Class */
private class Book {
/** Declare Variables */
private int isbn;
private String bookTitle;
private String authorName;
private int yearPublished;
private String publisherName;
private double bookPrice;
/** Constructor */
Book (int isbn, String bookTitle, String authorName, int yearPublished, String publisherName, double bookPrice) {
setIsbn(isbn);
setBookTitle(bookTitle);
setAuthorName(authorName);
setYearPublished(yearPublished);
setPublisherName(publisherName);
setBookPrice(bookPrice);
}
/**
* @return the isbn
*/
public void getIsbn(int isbn) {
return isbn;
}
/**
* @param isbn the isbn to set
*/
public void setIsbn(int isbn) {
this.isbn = isbn;
}
/**
* @return the bookTitle
*/
public void getBookTitle(String bookTitle) {
return bookTitle;
}
/**
* @param bookTitle the bookTitle to set
*/
public void setBookTitle(String bookTitle) {
this.bookTitle = bookTitle;
}
/**
* @return the authorName
*/
public String getAuthorName() {
return authorName;
}
/**
* @param authorName the authorName to set
*/
public void setAuthorName(String authorName) {
this.authorName = authorName;
}
/**
* @return the yearPublished
*/
public int getYearPublished() {
return yearPublished;
}
/**
* @param yearPublished the yearPublished to set
*/
public void setYearPublished(int yearPublished) {
this.yearPublished = yearPublished;
}
/**
* @return the publisherName
*/
public String getPublisherName() {
return publisherName;
}
/**
* @param publisherName the publisherName to set
*/
public void setPublisherName(String publisherName) {
this.publisherName = publisherName;
}
/**
* @return the bookPrice
*/
public double getBookPrice() {
return bookPrice;
}
/**
* @param bookPrice the bookPrice to set
*/
public void setBookPrice(double bookPrice) {
this.bookPrice = bookPrice;
}
} // End Book Class
/** Main Method */
public static void main(String[] args) {
} // End Main Method
} // End Bookstore Class
这就是我现在的处境。我再次陷入如何正确定义变量,然后在 Bookstore 方法中调用数据进行输出的问题。我知道如何将其打印到屏幕上,只是将其打印到屏幕上,这让我感到困惑。我是否要在 Bookstore 类中创建另一个 Book 对象?
我感谢任何可用的帮助。
最佳答案
让我们从...开始
private class Book {
我想说将内部类声明为 private
可能不是最佳选择,因为您无法在 BookStore
范围之外使用,但这是一个选择你需要做...
这个...
/**
* @return the isbn
*/
public void getIsbn(int isbn) {
return isbn;
}
在许多帐户上都是错误的。首先,它被声明为 void
,这意味着该方法不会返回任何内容,但随后您可以在该方法中使用 return isbn
。它应该被声明为返回一个int
。
接下来,您将 isbn
作为参数传递给该方法,但立即返回相同的值,这并不是您真正想要做的,您想要返回 的值isbn
由 Book
定义,例如...
/**
* @return the isbn
*/
public int getIsbn() {
return isbn;
}
同样的事情也适用于getBookTitle
。
在某些语言中,它们通过引用传递参数,这意味着您实际上可以更改方法/函数中的值,并且这将反射(reflect)在调用者上下文中。 Java 不会这样做(更准确地说,您无法在方法内重新分配值并将其反射(reflect)在调用者中)。这对许多开发者来说是一个转折点。
基本上,这意味着你不能做类似的事情......
public void getBookTitle(String bookTitle) {
bookTitle = this.bookTitle;
}
因为在方法调用之后的调用者上下文中参数的值将与之前的相同。您必须从类中“获取”值。
一个有趣的副作用是,如果传递给方法的对象提供可变功能,您可以更改它们的属性...
关于java - 关于在 Java 中调用构造函数和定义变量的问题(作业),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25612808/