构建器Constructor的返回值/构建器
构建器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的返回值/构建器的更多相关文章
- springMVC源码分析--HandlerMethodReturnValueHandlerComposite返回值解析器集合(二)
在上一篇博客springMVC源码分析--HandlerMethodReturnValueHandler返回值解析器(一)我们介绍了返回值解析器HandlerMethodReturnValueHand ...
- 基础才是重中之重~Emit动态构建方法(参数和返回值)
回到目录 对于Emit我们知道它的可以动态构建程序集,类型,方法,属性等,或者说只要手动使用C#创建的东西使用Emit也都可以动态创建它们,Emit由于它的特别之处,所以在很多领域得到了广泛的应用,像 ...
- python基础之函数进阶之函数作为返回值/装饰器
因为装饰器需要用到返回函数的知识,所以在这里将返回函数和装饰器合并讲解. 什么是返回函数? 我们知道,一个函数中return可以返回一个或者多个值,但其实,return不仅可以返回值,还可以返回函数. ...
- springMVC源码分析--HandlerMethodReturnValueHandler返回值解析器(一)
HandlerMethodReturnValueHandler是用于对Controller中函数执行的返回值进行处理操作的,springMVC提供了多个HandlerMethodReturnValue ...
- 值提供器 AND 模型绑定器
本章介绍了值提供器的作用,ASP MVC自带的5中值提供器.以及模型绑定器的作用,自定义模型绑定器并使用自定义的模型绑定器(类型上加上[ModelBinder(typeof(xx))]或者在全局模型绑 ...
- SpringMVC源码学习:容器初始化+MVC初始化+请求分发处理+参数解析+返回值解析+视图解析
目录 一.前言 二.初始化 1. 容器初始化 根容器查找的方法 容器创建的方法 加载配置文件信息 2. MVC的初始化 文件上传解析器 区域信息解析器 handler映射信息解析 3. Handler ...
- springMVC源码分析--ViewNameMethodReturnValueHandler返回值处理器(三)
之前两篇博客springMVC源码分析--HandlerMethodReturnValueHandler返回值解析器(一)和springMVC源码分析--HandlerMethodReturnValu ...
- python 通用装饰器,带有参数的装饰器,
# 使用装饰器对有返回值的函数进行装饰# def func(functionName): # print('---func-1----') # def func_in(): # print(" ...
- SpringMVC方法的返回值类型和自动装配
1. void类型作为返回值类型 /** * 如果方法写成了void就跟原来servlet含义是差不多 的 * json */ @RequestMapping("/firstRequest& ...
随机推荐
- c#_生成图片式验证码
废话不多说直接上代码. class Check_Code { /// <summary> /// 生成随机验证码数字+字母 /// </summary> /// <par ...
- --- rk3399/3288 系列平台接mipi 的dts 数据 panel-init-sequence = [] 命令的整法
https://blog.csdn.net/Shushan1/article/details/87858434 mipi 屏的数据手册 dts sample: &dsi { status = ...
- MySQL慢查询日志相关的文件配置和使用。
MySQL慢查询日志提供了超过指定时间阈值的查询信息,为性能优化提供了主要的参考依据,是一个非常实用的功能,MySQL慢查询日志的开启和配置非常简单,可以指定记录的文件(或者表),超过的时间阈值等就可 ...
- LevelDB源码分析-Open
Open LevelDB的初始化主要由Open函数实现: Status DB::Open(const Options &options, const std::string &dbna ...
- 折腾newifi3 d2笔记
1.忘记密码,恢复出厂 通电开机,等正常运行后,长按RESET大约6~8秒,见所有灯开始一起慢闪,可松手等重启就是出厂状态了,出厂IP是:192.168.99.1 2.免拆机刷breed 首先要打开s ...
- java 与 CDH kafka集成
本文主要是通过在网上找到的例子进行演示: 一.说明 开发环境如下: idea + jdk 1.8 + maven maven 中引用的架包如下: 二. 生产者 impor ...
- linux查看硬件详细信息dmidecode
[root@zabbix_server src]# dmidecode|more # dmidecode 2.12 SMBIOS 2.7 present. structures occupying b ...
- OpenStack 安装:neutron服务
在上一篇中介绍了Nova的安装配置,这一篇介绍neutron 首先,创建neutron用户并设置密码为neutron [root@linux-node1 ~]# openstack user crea ...
- php 配置xdebug
https://blog.csdn.net/Alan8865/article/details/81331252
- python之函数递归
函数递归调用 在函数内部,可以调用其它函数,如果一个函数在内部调用自身,即是递归调用 为防止无限递归类似于死循环,需要如下: 1.必须要有一个明确的返回值: 2.每次进入更深一层递归时,问题规模应该比 ...