文档章节

Returning a Value from a Method 方法的返回

zray4u
 zray4u
发布于 2016/08/16 09:55
字数 622
阅读 14
收藏 0

Returning a Value from a Method

A method returns to the code that invoked it when it

  • completes all the statements in the method,
  • reaches a return statement, or
  • throws an exception (covered later),

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.

Any method declared void doesn't return a value. It does not need to contain a return statement, but it may do so. 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;

If you try to return a value from a method that is declared void, you will get a compiler error.

Any method that is not declared void must contain a return statement with a corresponding return value, like this:

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:

// 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.

The getArea method returns a primitive type. A method can also return a reference type. For example, in a program to manipulate Bicycle objects, we might have a method like this:

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 seeWhosFastest(注:原文中是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. 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.

The class hierarchy for ImaginaryNumber

 

The class hierarchy for ImaginaryNumber

Now suppose that you have a method declared to return a Number:

public Number returnANumber() {
    ...
}

The returnANumber method can return an ImaginaryNumber but not an Object. (即返回值要与Number兼容)ImaginaryNumber is a Number because it's a subclass of Number. However, an Object is not necessarily a Number — it could be a String or another type.

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.

---------------------

泛型中的协变和逆变  https://msdn.microsoft.com/zh-cn/library/dd799517(v=vs.110).aspx

委托中的变体(C# 和 Visual Basic)  https://msdn.microsoft.com/zh-cn/library/dd233060.aspx

协变和逆变(C# 和 Visual Basic)  https://msdn.microsoft.com/zh-cn/library/ee207183.aspx

本文转载自:https://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html

共有 人打赏支持
zray4u
粉丝 0
博文 209
码字总数 188204
作品 0
西城
私信 提问
spring aop 申明了切面类之后,如何申明切入点呢?

8.2.3 Declaring a pointcut Recall that pointcuts determine join points of interest, and thus enable us to control when advice executes. Spring AOP only supports method execution......

Oscarfff
2015/04/08
0
0
[@Controller]4 详解@ModelAttribute

A、@ModelAttribute Annotation that binds a method parameter or method return value to a named model attribute, exposed to a web view. Supported for RequestMapping annotated han......

Edens_Song
2012/11/14
0
0
点点滴滴积累——基于XMLSchema与Annotation的几种Advice的规则

1.关于before advice: 对于before advice,是最简单的advice。无论是基于XmlSchema或者是基于Annotation,其advice对应的方法的参数只有一个,即JoinPoint类型的参数。在XmlSchema中其aop:b...

杨昊亚
2012/10/29
0
0
spring --(18)异常通知、返回通知、环绕通知

直接上代码,通俗易懂

求是科技
2016/09/01
13
0
Spring 如何处理Controller

一、首先说一下 @ModelAttribute @ModelAttribute 绑定请求参数到命令对象 @ModelAttribute一个具有如下三个作用: ①绑定请求参数到命令对象:放在功能处理方法的入参上时,用于将多个请求参...

Oscarfff
2015/06/02
0
0

没有更多内容

加载失败,请刷新页面

加载更多

如何解决 homebrew 更新慢的问题

之前一直困扰于 Homebrew 的更新速度,曾试过修改更新源(清华、中科大等)的方式,但是并没什么卵用;也试过设置 curl 代理的方式,但是 brew 走的好像不是 curl 的方式,所以也没用。 通过...

whoru
14分钟前
0
0
TiDB EcoSystem Tools 原理解读系列(二)TiDB-Lightning Toolset 介绍

简介 TiDB-Lightning Toolset 是一套快速全量导入 SQL dump 文件到 TiDB 集群的工具集,自 2.1.0 版本起随 TiDB 发布,速度可达到传统执行 SQL 导入方式的至少 3 倍、大约每小时 100 GB,适合...

TiDB
16分钟前
0
0
【Visual Studio 扩展工具】如何在ComponentOneFlexGrid树中显示RadioButton

概述 在ComponentOne Enterprise .NET控件集中,FlexGrid表格控件是用户使用频率最高的控件之一。它是一个功能强大的数据管理工具,轻盈且灵动,以分层的形式展示数据(数据呈现更加直观)。...

葡萄城技术团队
19分钟前
0
0
Maven环境隔离

Maven环境隔离 1. 什么是Maven环境隔离 顾名思义,Maven环境隔离就是将开发中的环境与beat环境、生产环境分隔开,方便进行开发和维护。这个在实际项目中用的还是很多的,如果你的项目用的Mav...

蚂蚁-Declan
19分钟前
1
0
day182-2018-12-19-英语流利阅读-待学习

“性感”时代已去,维密将如何转身? Daniel 2018-12-19 1.今日导读 维多利亚的秘密(Victoria's Secret)这个内衣品牌,最近似乎步入了“中年危机”——曾经打遍天下的“性感”内衣,在主打...

飞鱼说编程
19分钟前
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部