Documentation

The Java™ Tutorials
Hide TOC
Returning a Value from a Method从方法返回值
Trail: Learning the Java Language
Lesson: Classes and Objects
Section: More on Classes

Returning a Value from a Method从方法返回值

A method returns to the code that invoked it when it方法返回到调用它的代码

whichever occurs first.以先到者为准。

You declare a method's return type in its method declaration.在方法声明中声明方法的返回类型。Within the body of the method, you use the return statement to return the value.在方法体中,使用return语句返回值。

Any method declared void doesn't return a value.任何声明为void的方法都不会返回值。It does not need to contain a return statement, but it may do so.它不需要包含return语句,但可以这样做。In such a case, a return statement can be used to branch out of a control flow block and exit the method and is simply used like this:在这种情况下,可以使用return语句从控制流块中分支并退出该方法,其使用方式如下:

return;

If you try to return a value from a method that is declared void, you will get a compiler error.如果试图从声明为void的方法返回值,则会出现编译器错误。

Any method that is not declared void must contain a return statement with a corresponding return value, like this:任何未声明为void的方法都必须包含具有相应返回值的return语句,如下所示:

return returnValue;

The data type of the return value must match the method's declared return type; you can't return an integer value from a method declared to return a boolean.返回值的数据类型必须与方法声明的返回类型匹配;不能从声明为返回布尔值的方法返回整数值。

The getArea() method in the Rectangle Rectangle class that was discussed in the sections on objects returns an integer:Rectangle类中的getArea()方法返回一个整数,该方法在有关对象的章节中进行了讨论:

// a method for computing the area of the rectangle
    public int getArea() {
        return width * height;
    }

This method returns the integer that the expression width*height evaluates to.此方法返回表达式width*height计算结果的整数。

The getArea method returns a primitive type.getArea方法返回一个基元类型。A method can also return a reference type.方法还可以返回引用类型。For example, in a program to manipulate Bicycle objects, we might have a method like this:例如,在操纵Bicycle对象的程序中,我们可能有如下方法:

public Bicycle seeWhosFastest(Bicycle myBike, Bicycle yourBike,
                              Environment env) {
    Bicycle fastest;
    // code to calculate which bike is 
    // faster, given each bike's gear 
    // and cadence and given the 
    // environment (terrain and wind)
    return fastest;
}

Returning a Class or Interface返回类或接口

If this section confuses you, skip it and return to it after you have finished the lesson on interfaces and inheritance.如果本节内容让您感到困惑,请跳过本节内容,并在完成有关接口和继承的课程后返回本节内容。

When a method uses a class name as its return type, such as whosFastest does, the class of the type of the returned object must be either a subclass of, or the exact class of, the return type.当方法使用类名作为其返回类型时,例如whosFastest,返回对象类型的类必须是返回类型的子类或确切的类。Suppose that you have a class hierarchy in which ImaginaryNumber is a subclass of java.lang.Number, which is in turn a subclass of Object, as illustrated in the following figure.假设您有一个类层次结构,其中ImaginaryNumberjava.lang.Number的子类,而java.lang.Number又是Object的子类,如下图所示。

The class hierarchy for ImaginaryNumber

The class hierarchy for ImaginaryNumberImaginaryNumber的类层次结构

Now suppose that you have a method declared to return a Number:现在假设您声明了一个返回Number的方法:

public Number returnANumber() {
    ...
}

The returnANumber method can return an ImaginaryNumber but not an Object.returnANumber方法可以返回ImaginaryNumber,但不能返回对象。ImaginaryNumber is a Number because it's a subclass of Number.ImaginaryNumber是一个Number,因为它是Number的一个子类。However, an Object is not necessarily a Number — it could be a String or another type.但是,Object不一定是Number—它可以是String或其他类型。

You can override a method and define it to return a subclass of the original method, like this:您可以重写方法并将其定义为返回原始方法的子类,如下所示:

public ImaginaryNumber returnANumber() {
    ...
}

This technique, called covariant return type, means that the return type is allowed to vary in the same direction as the subclass.这种技术称为协变返回类型,意味着允许返回类型在与子类相同的方向上变化。


Note: You also can use interface names as return types.还可以使用接口名称作为返回类型。In this case, the object returned must implement the specified interface.在这种情况下,返回的对象必须实现指定的接口。

Previous page: More on Classes
Next page: Using the this Keyword