方法定义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;
}
}
答案 0 :(得分:1)
你知道它是一个构造函数,因为它与类本身的名称完全相同。
Class-name =构造函数名称
它不是一种方法,因为它不是一种方法。
在实例化Object时使用。
答案 1 :(得分:0)
构造函数不是它们不返回任何东西的方法