关于在Java中调用构造函数和定义变量的问题(家庭作业)
要明确的是,这是一个积极的家庭作业。我只是需要一些指导 我正在用Java创建一个书店程序,在修订版1中,该程序运行时只需返回一本特定书籍的信息。我必须使用两个类,并在main方法内的第二个类中调用info来输出 我认为我在Book类中正确地创建了构造函数,但是A)定义这些变量和B)调用Bookstore类中的信息进行输出的最佳方法是什么?我只是需要一些指导,因为我有点困了。Eclipse要求我将int和double变量重新定义为一个字符串,但它们将是数字……我的语法中有什么原因导致它这样做吗 以下是我目前掌握的情况:关于在Java中调用构造函数和定义变量的问题(家庭作业),java,Java,要明确的是,这是一个积极的家庭作业。我只是需要一些指导 我正在用Java创建一个书店程序,在修订版1中,该程序运行时只需返回一本特定书籍的信息。我必须使用两个类,并在main方法内的第二个类中调用info来输出 我认为我在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
,这意味着该方法不会返回任何内容,但随后在该方法中使用returnisbn
。它应该声明为返回一个int
接下来,您将isbn
作为参数传递给该方法,但立即返回相同的值,这不是您真正想要做的,您想要返回Book
定义的isbn
的值,例如
/**
* @return the isbn
*/
public int getIsbn() {
return isbn;
}
同样的事情也适用于getBookTitle
在某些语言中,它们通过引用传递参数,这意味着您可以实际更改方法/函数中的值,这将反映在调用方上下文中。Java不会这样做(更准确地说,您不能在方法中重新分配值并将其反映在调用方中)。这是许多开发人员的绊脚石
基本上,这意味着你不能做像
public void getBookTitle(String bookTitle) {
bookTitle = this.bookTitle;
}
因为在方法调用之后的调用方上下文中,参数的值将与调用之前的值相同。您必须从类中“获取”值
不过,一个有趣的副作用是,如果传递给方法的对象提供可变功能,则可以更改这些对象的属性…你在问什么?我只是想问一下为什么要写这本-->私有类书?我看不到真正的问题,而是问“最佳”方法不是很有建设性。有很多方法我认为我的问题很清楚,我很抱歉。你们的一些getter没有返回类型,但正在尝试返回值。例如,getIsnb不需要参数值,但应重新设置int,因此isbn的正确方法是:'code'public int getIsbn(int-isbn){return isbn;}/***@param isbn要设置的isbn*/public void setIsbn(int-isbn){this.isbn=isbn;}'code'