gpt4 book ai didi

java - 为什么方法定义头部没有 void 的 void 方法不会导致错误?

转载 作者:行者123 更新时间:2023-12-01 12:29:00 28 4
gpt4 key购买 nike

方法定义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;
}

}

最佳答案

您知道它是一个构造函数,因为它与类本身具有完全相同的名称。

类名=构造函数的名称

它不被用作一种方法,因为它不是一种方法。

它在实例化对象时使用。

关于java - 为什么方法定义头部没有 void 的 void 方法不会导致错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26102731/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com