文档章节

Returning a Value from a Method 方法的返回

zray4u
 zray4u
发布于 2016/08/16 09:55
字数 622
阅读 13
收藏 0
点赞 0
评论 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

© 著作权归作者所有

共有 人打赏支持
zray4u
粉丝 0
博文 159
码字总数 187679
作品 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

[@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

spring --(18)异常通知、返回通知、环绕通知

直接上代码,通俗易懂

求是科技 ⋅ 2016/09/01 ⋅ 0

点点滴滴积累——基于XMLSchema与Annotation的几种Advice的规则

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

杨昊亚 ⋅ 2012/10/29 ⋅ 0

Spring 如何处理Controller

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

Oscarfff ⋅ 2015/06/02 ⋅ 0

springmvc学习笔记(20)-拦截器

springmvc学习笔记(20)-拦截器 标签: springmvc [TOC] 本文主要介绍springmvc中的拦截器,包括拦截器定义和的配置,然后演示了一个链式拦截的测试示例,最后通过一个登录认证的例子展示了拦...

brianway ⋅ 2016/03/16 ⋅ 0

Fork/Join(3):ForkJoinPool之API翻译

  ForkJoinPool是Fork/Join框架的两大核心类之一,这一节先翻译API,具体使用方法和实现原理留待后续章节再谈。   虽然fork/join框架从JDK1.7开始就已经存在,但介绍Fork/Join框架的文章...

Xcafe ⋅ 2016/12/29 ⋅ 0

Spring笔记4---前置后置通知

开始聊聊注解! Spring切面可以应用5种类型的通知: 1 Before---在方法被调用之前调用通知 2 After--- 在方法完成之后调用通知,无论方法执行是否成功 3 After-returning---在方法成功执行之...

强子哥哥 ⋅ 2015/01/11 ⋅ 0

使用Druid但jndi报错

@wenshao 你好,想跟你请教个问题: 现在我使用springmvc配置数据源用的是mybatis但配置之后有异常,这个是配置文件

hailang2013 ⋅ 2013/10/21 ⋅ 0

Failed to convert value of type [java.lang.String] to required type [java.lang.Long]

2016-09-19 10:14:54.157 [http-nio-8080-exec-5] [DEBUG] org.springframework.web.servlet.DispatcherServlet {DispatcherServlet.java:845} - DispatcherServlet with name 'spring-servl......

落落酱 ⋅ 2016/09/19 ⋅ 2

没有更多内容

加载失败,请刷新页面

加载更多

下一页

大数据,必须掌握的10项顶级安全技术

我们看到越来越多的数据泄漏事故、勒索软件和其他类型的网络攻击,这使得安全成为一个热门话题。 去年,企业IT面临的威胁仍然处于非常高的水平,每天都会看到媒体报道大量数据泄漏事故和攻击...

p柯西 ⋅ 23分钟前 ⋅ 0

Linux下安装配置Hadoop2.7.6

前提 安装jdk 下载 wget http://mirrors.hust.edu.cn/apache/hadoop/common/hadoop-2.7.6/hadoop-2.7.6.tar.gz 解压 配置 vim /etc/profile # 配置java环境变量 export JAVA_HOME=/opt/jdk1......

晨猫 ⋅ 28分钟前 ⋅ 0

crontab工具介绍

crontab crontab 是一个用于设置周期性被执行的任务工具。 周期性执行的任务列表称为Cron Table crontab(选项)(参数) -e:编辑该用户的计时器设置; -l:列出该用户的计时器设置; -r:删除该...

Linux学习笔记 ⋅ 54分钟前 ⋅ 0

深入Java多线程——Java内存模型深入(2)

5. final域的内存语义 5.1 final域的重排序规则 1.对于final域,编译器和处理器要遵守两个重排序规则: (1)在构造函数内对一个final域的写入,与随后把这个被构造对象的引用赋值给一个引用...

江左煤郎 ⋅ 今天 ⋅ 0

面试-正向代理和反向代理

面试-正向代理和反向代理 Nginx 是一个高性能的反向代理服务器,但同时也支持正向代理方式的配置。

秋日芒草 ⋅ 今天 ⋅ 0

Spring 依赖注入(DI)

1、Setter方法注入: 通过设置方法注入依赖。这种方法既简单又常用。 类中定义set()方法: public class HelloWorldOutput{ HelloWorld helloWorld; public void setHelloWorld...

霍淇滨 ⋅ 昨天 ⋅ 0

马氏距离与欧氏距离

马氏距离 马氏距离也可以定义为两个服从同一分布并且其协方差矩阵为Σ的随机变量之间的差异程度。 如果协方差矩阵为单位矩阵,那么马氏距离就简化为欧氏距离,如果协方差矩阵为对角阵,则其也...

漫步当下 ⋅ 昨天 ⋅ 0

聊聊spring cloud的RequestRateLimiterGatewayFilter

序 本文主要研究一下spring cloud的RequestRateLimiterGatewayFilter GatewayAutoConfiguration @Configuration@ConditionalOnProperty(name = "spring.cloud.gateway.enabled", matchIfMi......

go4it ⋅ 昨天 ⋅ 0

Spring clound 组件

Spring Cloud技术应用从场景上可以分为两大类:润物无声类和独挑大梁类。 润物无声,融合在每个微服务中、依赖其它组件并为其提供服务。 Ribbon,客户端负载均衡,特性有区域亲和、重试机制。...

英雄有梦没死就别停 ⋅ 昨天 ⋅ 0

Confluence 6 重新获得站点备份文件

Confluence 将会创建备份,同时压缩 XML 文件后存储熬你的 <home-directory>/backups> 目录中。你需要自己访问你安装的 Confluence 服务器,并且从服务器上获得这个文件。 运行从 Confluence...

honeymose ⋅ 昨天 ⋅ 0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部