The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
Here is an example of a typical method declaration:下面是一个典型的方法声明示例:
public double calculateAnswer(double wingSpan, int numberOfEngines, double length, double grossTons) { //do the calculation here }
The only required elements of a method declaration are the method's return type, name, a pair of parentheses, 方法声明所需的唯一元素是方法的返回类型、名称、一对括号()
, and a body between braces, {}
.()
和大括号{}
之间的主体。
More generally, method declarations have six components, in order:更一般地说,方法声明有六个组件,顺序如下:
public
, private
, and others you will learn about later.public
、private
和其他您稍后将了解的内容。void
if the method does not return a value.void
。()
.()
括起来。Modifiers, return types, and parameters will be discussed later in this lesson.本课程后面将讨论修饰符、返回类型和参数。Exceptions are discussed in a later lesson.例外情况将在后面的课程中讨论。
The signature of the method declared above is:上述方法的签名为:
calculateAnswer(double, int, double, double)
Although a method name can be any legal identifier, code conventions restrict method names.虽然方法名称可以是任何合法标识符,但代码约定限制方法名称。By convention, method names should be a verb in lowercase or a multi-word name that begins with a verb in lowercase, followed by adjectives, nouns, etc.按照惯例,方法名称应该是一个小写的动词,或者是一个以小写的动词开头的多词名称,后跟形容词、名词等。In multi-word names, the first letter of each of the second and following words should be capitalized.在多单词名称中,第二个和后面的每个单词的第一个字母都应该大写。Here are some examples:以下是一些例子:
run runFast getBackground getFinalData compareTo setX isEmpty
Typically, a method has a unique name within its class. However, a method might have the same name as other methods due to method overloading.通常,方法在其类中具有唯一的名称。但是,由于方法重载,一个方法可能与其他方法具有相同的名称。
The Java programming language supports overloading methods, and Java can distinguish between methods with different method signatures.Java编程语言支持重载方法,Java可以区分具有不同方法签名的方法。This means that methods within a class can have the same name if they have different parameter lists (there are some qualifications to this that will be discussed in the lesson titled "Interfaces and Inheritance").这意味着,如果类中的方法具有不同的参数列表,则它们可以具有相同的名称(在标题为“接口和继承”的课程中将对此进行讨论)。
Suppose that you have a class that can use calligraphy to draw various types of data (strings, integers, and so on) and that contains a method for drawing each data type.假设您有一个类,该类可以使用书法绘制各种类型的数据(字符串、整数等),并且包含用于绘制每种数据类型的方法。It is cumbersome to use a new name for each method为每个方法使用新名称很麻烦for example, 例如,drawString
, drawInteger
, drawFloat
, and so on.drawString
、drawInteger
、drawFloat
等。In the Java programming language, you can use the same name for all the drawing methods but pass a different argument list to each method.在Java编程语言中,可以对所有绘图方法使用相同的名称,但可以向每个方法传递不同的参数列表。Thus, the data drawing class might declare four methods named 因此,数据绘图类可能声明四个名为draw
, each of which has a different parameter list.draw
的方法,每个方法都有不同的参数列表。
public class DataArtist { ... public void draw(String s) { ... } public void draw(int i) { ... } public void draw(double f) { ... } public void draw(int i, double f) { ... } }
Overloaded methods are differentiated by the number and the type of the arguments passed into the method.重载方法通过传入方法的参数的数量和类型来区分。In the code sample, 在代码示例中,draw(String s)
and draw(int i)
are distinct and unique methods because they require different argument types.draw(String s)
和draw(int i)
是不同且唯一的方法,因为它们需要不同的参数类型。
You cannot declare more than one method with the same name and the same number and type of arguments, because the compiler cannot tell them apart.不能用相同的名称、相同数量和类型的参数声明多个方法,因为编译器无法区分它们。
The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.在区分方法时,编译器不考虑返回类型,因此,即使它们具有不同的返回类型,也不能声明具有相同签名的两种方法。