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发行说明。
A method returns to the code that invoked it when it方法返回到调用它的代码
return
statement, orreturn
语句,或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; }
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.ImaginaryNumber
是java.lang.Number
的子类,而java.lang.Number
又是Object
的子类,如下图所示。
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.这种技术称为协变返回类型,意味着允许返回类型在与子类相同的方向上变化。