构建器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. P1100 高低位交换

    题目描述 给出一个小于2^{32}232的正整数.这个数可以用一个3232位的二进制数表示(不足3232位用00补足).我们称这个二进制数的前1616位为“高位”,后1616位为“低位”.将它的高低位 ...

  2. mysql左连接右连接(查询两张表不同的数据)

    有两张表:一张A表he一张B表 left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 :right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录: ...

  3. Java基础知识思维导图

     

  4. vlookup 公式使用及常见问题

    该函数的语法规则如下: VLOOKUP(lookup_value,table_array,col_index_num,range_lookup) 参数 简单说明 输入数据类型 lookup_value ...

  5. 解决ajax请求默认不支持重定向问题

    1,Ajax默认是不支持重定向的,只局部刷新数据,不跳转页面. 2,后台代码处理: @RequestMapping("/updateCurrentUser") public Str ...

  6. uni-app 使用 iconfont

    使用 uni-app 做项目时需要用到 iconfont.和 web 使用略有差别.谨以此记录. 因为 uni-app 不能使用本地字体图标库,所以不能直接下载使用. 1.将iconfont中需要的图 ...

  7. docker学习-lnmp+redis之搭建mysql容器服务

    一. 前期准备工作,创建配置文件目录,log文件目录,数据库DATA和WEB站点目录[root@T1 ~]# mkdir -p /lnmp/conf/{mysql,nginx,php} /lnmp/l ...

  8. Maven模块化开发

    Maven模块化开发 多人协同开发时,特别是规模较大的项目,为方便日后代码维护管理会将每个人的工作细分到具体的功能和模块上.随着项目的不断扩大,模块会越来越多,后续更加难以维护和扩展,为应对这种情况后 ...

  9. ucos中的中断管理

    一.中断的概念 中断是一种硬件机制,用于处理异步事件.中断的实时性比轮询要好,通过中断,微控制器可以在异常发生的时候立刻进行处理,而不需要不断轮询事件是否发生. CM3支持中断嵌套,使得高优先级异常可 ...

  10. web项目通过ajax提交数据太大报错

    通过ajax提交大数据 $.ajax({ url:"", data:{xx:xx} }) 这样子大大的字符串四五个一块提交.导致的提交的请求太大 idea报错 浏览器页面报错 解决 ...