构建器Constructor的返回值?

为什么会有这个问题?

在《Thinking in Java》中文Quanke翻译版本第四章初始化和清除,原书第五章Initialization&Cleanup中。关于用构建器自动初始化有如下描述:

构建器有助于消除大量涉及类的问题,并使代码更易阅读。例如在前述的代码段中,我们并未看到对initialize()方法的明确调用——那些方法在概念上独立于定义内容。在Java中,定义和初始化属于统一的概念——两者缺一不可。 构建器属于一种较特殊的方法类型,因为它没有返回值。这与void返回值存在着明显的区别。对于void返回值,尽管方法本身不会自动返回什么,但仍然可以让它返回另一些东西。构建器则不同,它不仅什么也不会自动返回,而且根本不能有任何选择。若存在一个返回值,而且假设我们可以自行选择返回内容,那么编译器多少要知道如何对那个返回值作什么样的处理。

Constructors eliminate a large class of problems and make the code easier to read. In the preceding code fragment, for example, you don’t see an explicit call to some initialize( ) method that is conceptually separate from creation. In Java, creation and initialization are unified concepts—you can’t have one without the other.

The constructor is an unusual type of method because it has no return value. This is distinctly different from a void return value, in which the method returns nothing but you still have the option to make it return something else. Constructors return nothing and you don’t have an option (the new expression does return a reference to the newly created object, but the constructor itself has no return value). If there were a return value, and if you could select your own, the compiler would somehow need to know what to do with that return value.

另外在《The Java Programming Language》

For purposes other than simple initialization, classes can have constructors. Constructors are blocks of statements that can be used to initialize an object before the reference to the object is returned by new. Constructors have the same name as the class they initialize. Like methods, they take zero or more arguments, but constructors are not methods and thus have no return type. Arguments, if any, are provided between the parentheses that follow the type name when the object is created with new. Constructors are invoked after the instance variables of a newly created object of the class have been assigned their default initial values and after their explicit initializers are executed.

大致这样写,尽管方法本身不会自动返回什么,但仍然可以让它返回另一些东西。关于构建器有返回值的证据无非是这样:

class Rock {
Rock(int i) {
System.out.println(
"Creating Rock number " + i);
}
}
public class SimpleConstructor {
public static void main(String[] args) {
for(int i = 0; i < 10; i++)
new Rock(i);
}
}

当用new来构建一个Tree对象的时候:

tree t = new Tree(12); // 12英尺高的树

这样来看构建Tree对象的时候的确返回了内容。

首先需要了解new这个关键字但到底做了什么?因为篇幅有限,可以访问ORACLE官方的JavaDocumention(点击JavaDocumention可以浏览英文原版),翻译版本(点击翻译版本文本浏览)

构建器Constructor的返回值/构建器的更多相关文章

  1. springMVC源码分析--HandlerMethodReturnValueHandlerComposite返回值解析器集合(二)

    在上一篇博客springMVC源码分析--HandlerMethodReturnValueHandler返回值解析器(一)我们介绍了返回值解析器HandlerMethodReturnValueHand ...

  2. 基础才是重中之重~Emit动态构建方法(参数和返回值)

    回到目录 对于Emit我们知道它的可以动态构建程序集,类型,方法,属性等,或者说只要手动使用C#创建的东西使用Emit也都可以动态创建它们,Emit由于它的特别之处,所以在很多领域得到了广泛的应用,像 ...

  3. python基础之函数进阶之函数作为返回值/装饰器

    因为装饰器需要用到返回函数的知识,所以在这里将返回函数和装饰器合并讲解. 什么是返回函数? 我们知道,一个函数中return可以返回一个或者多个值,但其实,return不仅可以返回值,还可以返回函数. ...

  4. springMVC源码分析--HandlerMethodReturnValueHandler返回值解析器(一)

    HandlerMethodReturnValueHandler是用于对Controller中函数执行的返回值进行处理操作的,springMVC提供了多个HandlerMethodReturnValue ...

  5. 值提供器 AND 模型绑定器

    本章介绍了值提供器的作用,ASP MVC自带的5中值提供器.以及模型绑定器的作用,自定义模型绑定器并使用自定义的模型绑定器(类型上加上[ModelBinder(typeof(xx))]或者在全局模型绑 ...

  6. SpringMVC源码学习:容器初始化+MVC初始化+请求分发处理+参数解析+返回值解析+视图解析

    目录 一.前言 二.初始化 1. 容器初始化 根容器查找的方法 容器创建的方法 加载配置文件信息 2. MVC的初始化 文件上传解析器 区域信息解析器 handler映射信息解析 3. Handler ...

  7. springMVC源码分析--ViewNameMethodReturnValueHandler返回值处理器(三)

    之前两篇博客springMVC源码分析--HandlerMethodReturnValueHandler返回值解析器(一)和springMVC源码分析--HandlerMethodReturnValu ...

  8. python 通用装饰器,带有参数的装饰器,

    # 使用装饰器对有返回值的函数进行装饰# def func(functionName): # print('---func-1----') # def func_in(): # print(" ...

  9. SpringMVC方法的返回值类型和自动装配

    1. void类型作为返回值类型 /** * 如果方法写成了void就跟原来servlet含义是差不多 的 * json */ @RequestMapping("/firstRequest& ...

随机推荐

  1. mount命令和自动挂载实例

    前言 介绍mount命令和一个实例. mount命令 作用 作用:挂载linux系统外的文件 命令格式 mount [-hV] mount -a [-fFnrsvw] [-t vfstype] mou ...

  2. 字母导航跳转react核心代码

    componentDidMount() { this.move(); } skipToDep(e) { let dom = document.getElementById(e); // 获取要跳至的字 ...

  3. 有哪些api接口可以实现微信自动唤醒浏览器,下载app,打开网页

    现在微信渠道可以说是拉新最快的渠道,因为微信具备强裂变性.但是目前微信对第三方下载链接的拦截是越来越严格了,那么想要在微信内肆无忌惮地推广链接就需要用到微信跳转浏览器的接口,那如何获取该接口呢?   ...

  4. ubuntu16下安装openssh

    由于SecureCRT.xshell远程连接ubuntu是通过ssh协议的,所以,需要给ubuntu安装ssh服务器. 1. ssh协议科普 Secure Shell(缩写为SSH),由IETF的网络 ...

  5. Windows下nginx下安装amqp

    1.复制php_amqp.dll 到php/ext/ 2.复制rabbitmq.4.dll 到php/3.复制rabbitmq.4.dll 到windows/system32/ (如32位) ,64位 ...

  6. 服务主机superFetch占用磁盘过多

    有用户升级到Win10系统后,发现系统运行不快,查看任务管理器中正在运行的进程,然后查看到有一个名称为“服务主机:本地系统(网络受限)”的进程的磁盘占用率很高.如果想要解决这个问题,那么不妨参考下面介 ...

  7. Adobe Flash player ActiveX下载地址

    http://www.adobe.com/support/flashplayer/downloads.html http://download.macromedia.com/pub/flashplay ...

  8. ssh登录locale报错:cannot change locale (zh_CN.UTF-8): No such file or directory

    一.登录ssh报错: Last :: from 172.28.146.109 -bash: warning: setlocale: LC_ALL: cannot change locale (en_C ...

  9. mysql访问视图提示:找不到视图

    原因: 1.不存在 2.视图区分大小写(有的不区分) 3.权限问题

  10. day51 JS基础

    复习 1.字体图标 用i标签, 设置类名, 与第三方字体图标库进行图标匹配 <link rel="stylesheet" href="font-awesome-4. ...