构造方法带参数的目的:是可以在创建对象的过程中就对对象的属性进行赋值操作。
以后在开发中使用别人写的类时,就需要查看其提供什么样的构造方法,才能通过构造方法实例化对象去使用。作者: 杨冉 时间: 2013-3-16 11:26
答案是不能,没有返回值和返回值为void是两种概念
《Thinking in java》这本书里头是这么解释的:
"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."
总结下原因就是,构造函数是完全不能够返回值的,我们没有选择。而void是我们有选择的不返回一个值。只有这样区分了,虚拟机才知道哪个是构造函数。
我们可以写段代码来看返回类型为void时是否能作为构造函数被调用: