文档章节

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
博文 208
码字总数 187679
作品 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
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
spring --(18)异常通知、返回通知、环绕通知

直接上代码,通俗易懂

求是科技
2016/09/01
13
0
点点滴滴积累——基于XMLSchema与Annotation的几种Advice的规则

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

杨昊亚
2012/10/29
0
0
Spring 如何处理Controller

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

Oscarfff
2015/06/02
0
0

没有更多内容

加载失败,请刷新页面

加载更多

帧动画(wifi信号动态动画)

准备六张wifi不同信号强度的素材图片,复制到drawable目录中 在drawable目录中新建frame文件,并编写代码 <animation-list xmlns:android="http://schemas.android.com/apk/res/android"> ...

lanyu96
27分钟前
1
0
快速get到学习Linux操作系统的点

Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户、多任务、支持多线程和多CPU的操作系统。Linux能够运行主要的UNIX工具软件、网络协议和应用程序。它支持32位...

问题终结者
35分钟前
1
0
Django2 model操作数据库

1.将应用(如learn)添加到安装应用配置中 将我们新建的应用(learn)添加到 settings.py 中的 INSTALLED_APPS中,告诉Django有这么一个应用。 INSTALLED_APPS = [ 'django.contrib.ad...

MichaelShu
37分钟前
1
0
SpringBoot基础篇Bean之条件注入之注解使用

更多Spring文章,欢迎点击 一灰灰Blog-Spring专题 bean的条件注入,除了前面一篇博文中介绍的通过@Conditional注解配合Condition接口的实现之外,还提供了更多简化的注解使用方式,省略了自己...

小灰灰Blog
48分钟前
1
0
git clone 报错 Clone failed: Authentication failed for

新装了系统,git和phpstorm的配置全都没有了,重新安装了git和phpstorm 系统:windows10 代码托管平台:阿里云code 软件的安装不作赘述。 报错1 Could not read from remote repository 解决...

Marhal
49分钟前
1
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部