The Java™ Tutorials > Learning the Java Language > Classes and Objects > Classes > Defining Methods

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. 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”).

Java 编程语言支持重载方法,Java 可以区分具有不同方法签名的方法。这意味着,如果类中的方法具有不同的参数列表,则它们可以具有相同的名称(对此有一些限制条件,将在“接口和继承”一课中讨论)。

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. 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. Thus, the data drawing class might declare four methods named draw, each of which has a different parameter list.

假设您有一个类,它可以使用书法来绘制各种类型的数据(字符串、整数等),并且包含绘制每种数据类型的方法。为每个方法使用新名称是很麻烦的 —— 例如,drawStringdrawIntegerdrawFloat,等等。在 Java 编程语言中,可以为所有绘图方法使用相同的名称,但向每个方法传递不同的参数列表。因此,数据绘制类可以声明四个名为 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.

注意:应谨慎使用重载方法,因为它们会使代码可读性大大降低。


原文

The Java™ Tutorials > Learning the Java Language > Classes and Objects > Classes > Defining Methods

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值