日期:2014-05-20  浏览次数:20677 次

再论构造函数有没有返回值??
我告诉他说:构造函数没有返回值。
他举了个反例:
  String str = new String("content");

new 的时候调用构造函数,返回给str;

再问:有没有方法确定知道构造函数有没有返回??

------解决方案--------------------
语法上没有
------解决方案--------------------
你咋这么固执呢 呵呵
就是没有返回任何类型 但是构造函数确实创建了一个新的对象
------解决方案--------------------
没有,但我不知怎么反驳你这个反例
------解决方案--------------------
String str=new String("content");
这里不是说new的时候调用构造函数,返回给str,而是在堆里new一个String对象,并让str指向这个对象。
构造方法没有返回值。
------解决方案--------------------
看书吧,反正见过的书上都是说构造方法没有返回值。
这是<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.
看最后一句话,构造方法是新创建的对象的实例变量缺省初始化以及显式初始化之后才执行的,也就是说在构造方法调用之前这个对象已经存在了,更谈不上你例子中的构造方法返回一个对象引用了。
------解决方案--------------------
为构造函数加上返回值后,也没有错,但是此时该函数已经变成类的成员方法,而不是构造函数
------解决方案--------------------
We create the object sun refers to using new. The new construct is by far the most common way to create objects (we cover the other ways in Chapter 16). When you create an object with new, you specify the type of object you want to create and any arguments for its construction. The runtime system allocates enough space to store the fields of the object and initializes it in ways you will soon see. When initialization is complete, the runtime system returns a reference to the new object.
------解决方案--------------------
构造方法是一种特殊的方法,具有以下特点。
(1)构造方法的方法名必须与类名相同。
(2)构造方法没有返回类型,也不能定义为void,在方法名前面不声明方法类型。
(3)构造方法的主要作用是完成对象的初始化工作,它能够把定义对象时的参数传给对象的域。
(4)构造方法不能由编程人员调用,而要系统调用。
(5)一个类可以定义多个构造方法,如果在定义类时没有定义构造方法,则编译系统会自动插入一个无参数的默认构 造器,这个构造器不执行任何代码。
(6)构造方法可以重载,以参数的个数,类型,或排列顺序区分。 

我个人认为规定是没有返回的,但实际是隐式返回的类的对象。

------解决方案--------------------
还是那句话..

既然名字叫 构造函数. 他就是函数..函数有没有返回值 就看有没有return.这个大家没意见吧.

如果他还是坚持它的..让他自己给 构造函数里面 加 return吧.

能加上的话. 我给他100分..加不上给我分.

o(∩_∩)o....
------解决方案--------------------
这就像1+1=2,知道就行了.没有必要去怀疑他,有那时间还不如看看JAVA的源码呢.
------解决方案--------------------
构造函数是没返回值的 ,但是返回引用 你可以在这上找出路
------解决方案--------------------
探讨
可我真想搞清楚。。。期待大侠

------解决方案--------------------
本人的理解是,构造函数,就是java中一个特殊的方法,只需要按照本本上的说明来用就是了,就象你吃感冒清一样,不需要去做化学分析,吃下去就够了,不能以常规的,指定了类型的普通方法来与之相提并论,如果一定要说个子丑寅卯,那就打电话去sun吧
------解决方案--------------------
探讨
构造方法是一种特殊的方法,具有以下特点。