Documentation

The Java™ Tutorials
Hide TOC
Defining Methods定义方法
Trail: Learning the Java Language
Lesson: Classes and Objects
Section: Classes

Defining Methods定义方法

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:更一般地说,方法声明有六个组件,顺序如下:

  1. Modifiers修饰符such as public, private, and others you will learn about later.例如publicprivate和其他您稍后将了解的内容。
  2. The return type返回类型the data type of the value returned by the method, or void if the method does not return a value.方法返回的值的数据类型,如果方法不返回值,则为void
  3. The method name方法名the rules for field names apply to method names as well, but the convention is a little different.字段名的规则也适用于方法名,但约定略有不同。
  4. The parameter list in parenthesis括号中的参数列表a comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses, ().以逗号分隔的输入参数列表,前面是它们的数据类型,用括号()括起来。If there are no parameters, you must use empty parentheses.如果没有参数,则必须使用空括号。
  5. An exception list—to be discussed later.例外列表—待以后讨论。
  6. The method body, enclosed between braces—the method's code, including the declaration of local variables, goes here.方法主体,封闭在大括号之间—该方法的代码(包括局部变量的声明)如下所示。

Modifiers, return types, and parameters will be discussed later in this lesson.本课程后面将讨论修饰符、返回类型和参数。Exceptions are discussed in a later lesson.例外情况将在后面的课程中讨论。


Definition:定义: Two of the components of a method declaration comprise the method signature—the method's name and the parameter types.方法声明的两个组件包括方法签名—方法的名称和参数类型。

The signature of the method declared above is:上述方法的签名为:

calculateAnswer(double, int, double, double)

Naming a Method命名方法

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.通常,方法在其类中具有唯一的名称。但是,由于方法重载,一个方法可能与其他方法具有相同的名称。

Overloading Methods重载方法

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.例如,drawStringdrawIntegerdrawFloat等。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.在区分方法时,编译器不考虑返回类型,因此,即使它们具有不同的返回类型,也不能声明具有相同签名的两种方法。


Note: Overloaded methods should be used sparingly, as they can make code much less readable.重载方法应该少用,因为它们会使代码的可读性大大降低。

Previous page: Declaring Member Variables
Next page: Providing Constructors for Your Classes