【发布时间】:2014-11-24 00:18:56
【问题描述】:
方法定义LibraryBook 是一个void 方法,但它的定义头部没有void。为什么它不会导致错误?我尝试了另一种方法定义,而另一种确实导致了错误:invalid method declaration。
/**
/The following is a definition of the LibraryBook class.
/This class has four instance variables and one static (or class) variable.
/One of the instance variables is a type Author.
**/
public class LibraryBook
{
private int bookId;
private String bookName;
private Author bookAuthor;
private double bookPrice;
private static double totalInvValue;
public LibraryBook (int id, String name, Author author, double price)
{
bookId = id;
bookName = name;
bookAuthor = author;
bookPrice = price;
totalInvValue += price;
}
public String toString()
{
String response = "";
response += "Book Name is: " + bookName;
response += "\nIt costs: " + bookPrice;
response += "\nIt was " + bookAuthor.toString();
return response;
}
public static double getTotValue()
{
return totalInvValue;
}
}
【问题讨论】:
-
因为
LibraryBook()是一个构造函数,构造函数不返回任何内容,甚至不返回void。它不同于 普通 方法。