看书吧,反正见过的书上都是说构造方法没有返回值。 这是<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. 看最后一句话,构造方法是新创建的对象的实例变量缺省初始化以及显式初始化之后才执行的,也就是说在构造方法调用之前这个对象已经存在了,更谈不上你例子中的构造方法返回一个对象引用了。
new String("content"); 他们认定是构造函数的返回结果。。 哇,说服不了。。 大侠们高见啊 另外,我觉得这是new 函数的返回结果。这样可以说吗??
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.
new 运算符返回的对象引用或者指针 new负责在堆上分配对象所需的内存空间 构造函数负责在此位置构造对象
构造函数是可以显式返回的, 可以return;不能有返回值而已。
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. 6楼说的这么清楚了,楼主还不明白,我看学习不该是这么学的。原理才是王道
这里不是说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.
看最后一句话,构造方法是新创建的对象的实例变量缺省初始化以及显式初始化之后才执行的,也就是说在构造方法调用之前这个对象已经存在了,更谈不上你例子中的构造方法返回一个对象引用了。
他们认定是构造函数的返回结果。。
哇,说服不了。。
大侠们高见啊
另外,我觉得这是new 函数的返回结果。这样可以说吗??
(1)构造方法的方法名必须与类名相同。
(2)构造方法没有返回类型,也不能定义为void,在方法名前面不声明方法类型。
(3)构造方法的主要作用是完成对象的初始化工作,它能够把定义对象时的参数传给对象的域。
(4)构造方法不能由编程人员调用,而要系统调用。
(5)一个类可以定义多个构造方法,如果在定义类时没有定义构造方法,则编译系统会自动插入一个无参数的默认构 造器,这个构造器不执行任何代码。
(6)构造方法可以重载,以参数的个数,类型,或排列顺序区分。 我个人认为规定是没有返回的,但实际是隐式返回的类的对象。
<mx:HDividedBox x="0" y="41" width="100%" height="100%">
<mx:Panel width="220" height="100%" layout="absolute" id="play_file_list" title="播放列表">
<mx:FileSystemList x="0" y="0" width="100%" height="60%" id="file_list" extensions="{fExt}" enumerationMode="filesOnly" doubleClick="to_play_2(event);"/>
<mx:FileSystemTree id="file_tree"
width="100%" height="36%"
enumerationMode="directoriesOnly"
change="file_list.directory = File(file_tree.selectedItem);" x="0" bottom="0"/>
</mx:Panel>
<mx:Panel width="80%" height="100%" layout="absolute" id="play_windows">
<mx:VideoDisplay x="0" y="0" width="100%" height="100%" id="my_video" backgroundColor="#FFFFFF" complete="do_palyback(event);"/>
</mx:Panel>
</mx:HDividedBox>
<mx:ApplicationControlBar x="0" y="0" id="AppContBar" width="100%" height="43">
<mx:RadioButtonGroup id="palyback_model"/>
<mx:Button label="播放" id="btn_play" click="to_play(event);"/>
<mx:Button label="停止" id="btn_stop" click="video_stop(event);"/>
<mx:RadioButton label="連續播放" groupName="palyback_model" id="rbtn_paly_1" selected="true"/>
<mx:RadioButton label="單曲重復播放" groupName="palyback_model" id="rbtn_paly_2"/>
<mx:Button label="截圖" id="btn_Pic"/>
<mx:Button label="+" id="btn_room_big" click="v_room_big(event);"/>
<mx:Button label="-" id="btn_room_small" click="v_room_small(event);"/>
<mx:HSlider id="hs" minimum="0" maximum="{my_video.totalTime}"
thumbPress="thumbPress(event)"
thumbRelease="thumbRelease(event)"
change="thumbChanges(event)" />
<mx:HSlider id="hs_sound" width="80"
minimum="0" maximum="1"
thumbRelease="sound_thumbRelease(event)"
change="sound_thumbChanges(event)"
value="{my_video.volume}" />
<mx:Label id="playtime"
text="{formatTimes(my_video.playheadTime)} : {formatTimes(my_video.totalTime)}"
color="#ffffff"/>
</mx:ApplicationControlBar></mx:WindowedApplication>在網上找的一段播放mp3<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
width="400" height="110" layout="vertical"
horizontalAlign="center" verticalAlign="center"
>
<mx:Script>
<![CDATA[
import mx.core.SoundAsset;
import flash.media.*; [Embed(source="song.mp3")]
[Bindable]
public var Song:Class;
public var mySong:SoundAsset = new Song() as SoundAsset;
public var channel:SoundChannel;
public function playSound():void
{
// 先 停止
stopSound();
// 在播放
channel = mySong.play(0,int.MAX_VALUE);
}
public function stopSound():void
{
// 停止
if ( channel != null ) channel.stop();
}
]]>
</mx:Script>
<mx:HBox>
<mx:Button label="play" click="playSound();"/>
<mx:Button label="stop" click="stopSound();"/>
</mx:HBox>
</mx:Application>
String str="abc",这就像一个值类型,String str=new String("abc")就像引用类型!
但实际使用过程中我认为是有返回值的。
这里面应该是有一些jvm级别的东西
public Long String(){
} 这个是一个合法的Java函数了。而不是构造方法了。虽然不建议这么做。
并且把整个这个内存空间的地址(引用)返回给str,
而并不仅仅是返回构造函数构造的一些变量的值。希望你能理解
===>
B *a = (B *)malloc(sizeof(B));
a->B();
................................
返回的是new的對象本身
這是最特殊的返回函數,不必寫返回值,什麽void,int什麽的都不用寫,返回的一定是對象本身
感覺這么理解
可否?
返回值 = return value
构造函数有return 一个value么
对象下面的 TOSTRING方法
如果一个对象赋值给STRING变量是,默认调用该对象的TOSTRING方法。
TOSTRING方法是OBJECT一个公共方法其他对象都继承了这个方法。。
尼可以试试,定义一个类然后实例化一个类的对象,让一个字符串=对象的TOSTRING方法
这个结果和你的例子结果一样。
{
public static void main(String []args)
{
A xx = new A();
}
}
A的构造函数返回自身对象。。
new
dup
调用init方法
astore
C++按道理说是有返回的,返回值是this指针,只不过没写出来。
一个简单的默认构造函数
class A
{
A::A() {}
...OD调试的如下
00401000 /$ 55 push ebp
00401001 |. 8BEC mov ebp, esp
00401003 |. 51 push ecx //thiscall调用约定,先前call之前传递给ecx的类的this指针
00401004 |. 894D FC mov dword ptr [ebp-4], ecx
00401007 |. 8B45 FC mov eax, dword ptr [ebp-4] //返回值放入eax,可见是this指针
0040100A |. 8BE5 mov esp, ebp
0040100C |? 5D pop ebp
0040100D |? C3 retn
new负责在堆上分配对象所需的内存空间
构造函数负责在此位置构造对象
6楼说的这么清楚了,楼主还不明白,我看学习不该是这么学的。原理才是王道
其实是静态函数
首先证明是一个静态的公开函数.没有实际的对象实例就可以调用的类成员函数,一定是static函数,在被类加载器载入后就可以调用了。但构造函数可以在没创建实例就可以调用,所以一定是static函数。
静态函数为什么不用static声明呢?这与构造函数没有声明返回类型是一样的答案。反正是人为确定的规则,特殊的函数特殊的规则,没什么特别的。
也不能这么讲, 说不定引起人家学习和研究编译器的构造进而自己也著手写编译器也不定呢?
其实编译器只能从语法上限制我们不能做什么, 如果我们想做, 大可以绕过它, 就像没有所谓的const不能改写一样, 跑起来什么都可以改写, 只要固化(于固件上)的程序没有限制你(比如保护模式)的话. (你的程序下还有OS呢, OS下还有IA32<如果是IA32架构的话>呢, 如果你都能突破---你不会再问这个问题.)
就是从理解基本数据类型开始。首先必须明白类也是数据类型,不过是一种可综合各种基本数据类型于一身的复合型数据类型,但总之它也不过是数据类型,就好比头脑简单和头脑复杂的人都是人一样。其次既然是数据类型,那它就跟基本数据类型是一样的,那从编译其的角度来看,你程序员既然要我为你开辟一块内存空间,你总得有个跟我沟通的方法吧,那就是通常我们所说的定义变量,如:int A;但这时编译器虽然知道了你的意图,也可以为你开辟一块空间,但A里到底要存什么数呢,你没告诉它对吧,但这也没关系,通常你没告诉它之前编译器会做一些缺省处理,比如暂时放入0,或者不管它,原来是什么就是什么,这就是所谓编译器的自动初始化,但如果你想主动给A放入什么那当然是可以的,比如你可以这样定义:int A=5;或者你在别的地方需要用到时你再给它赋值也一样。说到此就可以用这个基本数据类型的定义过程跟类的构造函数过程相比较了:在你只定义int A,而没有给它赋值时,就当你忘记写了构造函数,这时编译器会自动给你做,而当你主动这么写时:int A=5,就相当于你主动写了构造函数,给了它赋了初始值。
构造方法没有返回值。同意5楼!!!就是一个引用str指向一个实例化对象,对象的实例化时用到了 构造函数!最后的结果就是你的str指向了这个实例化的对对象!如果有人,硬要说是构造函数返回的,那也没办法了!觉得这个问题,就是个人的理解了!怎么有点感觉像辩论似的呢!没意义的!好好看看书!该懂的时候,自然就懂了!
同时也证明了我们c++版的基础比你们好:
String str = new String("content"); 第一:首先申请内存,比如用:malloc,::operator new等。
第二:在这个申请内存的地址上,用定位new,放上这个对象,这时就调用构造函数。而定位new返回这个内存
的地址。
说没有
构造函数.在英文里叫Constructor
叫构造器更加准确.
不是函数。更没有返回值这一说
这并不算返回值,你可以这样想:什么是返回值? 返回值,可以自己任意定义返回的类型,可以自己任意定义返回的大小甚至是null. 但是new就不可以。new 会创建一个对象,并且返回对象的地址,也可以认为是引用。这不是返回值,而是new关键字本来的功能