Topic: 【请教】关于this的理解!! |
Print this page |
1.【请教】关于this的理解!! | Copy to clipboard |
Posted by: prettyxx Posted on: 2004-12-03 17:41 刚开始学JAVA,学到this这里,看了几本书上相关的内容,都没弄明白,以下面这个简单的程序为例: class A{ 这里的this是指什么,一个对象吗(看this(0)似乎又不是)?那这个对象是用的哪个构造函数呢?这个对象是在什么时候产生的呢?如果不是一个对象,那它又是什么呢? |
2.Re:【请教】关于this的理解!! [Re: prettyxx] | Copy to clipboard |
Posted by: ww1ww1 Posted on: 2004-12-04 09:56 this是指class本身,这样的: 首先,我们new 一个instance。 A a = new A(); 这样,我们auto调用了A的构造函数(constructor): public A(){ this --> A 也就是说,this(0) --> A(0),这样又调用了另一个带参数的A class constructor public A(int flag){ 将0传给flag, this.flag = flag <-- 0 就是A.flag = 0。 我们可以看出,用this只是为了表明flag是class A 的 flag 而不是参数 parameter的 int flag. |
3.Re:【请教】关于this的理解!! [Re: ww1ww1] | Copy to clipboard |
Posted by: prettyxx Posted on: 2004-12-04 12:17 如果“this是指class本身”那么类A非静态类,怎么能用this.flag,这么看,this还是一个对象才是,那么“this(0);”和“this.flag = flag;”这里两个this应该是两个不同的对象才是,因为用了不同的构造函数,那么这两个对象是在什么时候产生的呢,我不明白 |
4.Re:【请教】关于this的理解!! [Re: prettyxx] | Copy to clipboard |
Posted by: ww1ww1 Posted on: 2004-12-04 13:01 this是“这里、这是”的意思,也就是说,这里的(A class的)flag 等于 flag(parameter) |
5.Re:【请教】关于this的理解!! [Re: prettyxx] | Copy to clipboard |
Posted by: jigsaw Posted on: 2004-12-04 13:09 Post is deleted |
6.Re:【请教】关于this的理解!! [Re: jigsaw] | Copy to clipboard |
Posted by: ww1ww1 Posted on: 2004-12-04 13:16 Post is deleted |
7.Re:【请教】关于this的理解!! [Re: ww1ww1] | Copy to clipboard |
Posted by: prettyxx Posted on: 2004-12-04 13:32 ww1ww1 wrote: 你说的这个我明白,我不明白的是会不会由于类中有多个构造函数而导致this会指向不同的对象呢? |
8.Re:【请教】关于this的理解!! [Re: prettyxx] | Copy to clipboard |
Posted by: prettyxx Posted on: 2004-12-04 13:46 啊,我好像有些理解了,是不是说只有当这个类进行实例化产生对象时,this就指向该对象了,之前this不指向任何对象,是这样吗? |
9.Re:【请教】关于this的理解!! [Re: prettyxx] | Copy to clipboard |
Posted by: ww1ww1 Posted on: 2004-12-04 17:51 其实,可能我说的不够明白。 这样说会好些:this.flag(这里的flag)= flag((int flag))。你可以这样想,this.flag根本就和什么对象的这些概念完全没有关系,只是表明这个flag 是 class A 的一个属性int flag。 这个程序你看下:
|
10.Re:【请教】关于this的理解!! [Re: prettyxx] | Copy to clipboard |
Posted by: ww1ww1 Posted on: 2004-12-04 17:57 然后,我们可以在这样做
|
11.Re:【请教】关于this的理解!! [Re: prettyxx] | Copy to clipboard |
Posted by: prettyxx Posted on: 2004-12-04 22:18 感谢ww1ww1,请再看一下下面这个代码: class Container { Component comp; public void addComponent() { comp=new Component(this); } } class Component { Container myContainer; public Component(Container c) { myContainer=c; } } 请问这里的this是不是传递的是对象,而不是当前类 |
12.Re:【请教】关于this的理解!! [Re: prettyxx] | Copy to clipboard |
Posted by: ww1ww1 Posted on: 2004-12-05 00:19
类与对象不是一个概念。 对象是现实生活的某事物的抽象,好比:人 是一个对象,汽车 是一个对象 也就是 Object。人、汽车都是Object,所以Object是所有class的父类! 好了,现在有一个Container对象,我们怎么把它在程序语言中表述出来呢。我们用Class来描述它。Container这个对象(class)有它的属性:component,还有它的行为动作addComponent。 其实,可以用一个程序来描述它们之间的行为。让我想想,那位大哥想到好的描述程序。写出来观赏观赏。 |
13.Re:【请教】关于this的理解!! [Re: ww1ww1] | Copy to clipboard |
Posted by: prettyxx Posted on: 2004-12-05 00:41 ww1ww1 wrote: 谢谢你这么形象的描述了类与对象,但我依旧没有完全理解这段程序。 comp=new Component(this); this在这里是把Container的一个对象作为参数传过去了,也就是说这里的this是指向一个Container的对象,是这样理解吗?如何是,那这个Container的对象是在什么时候产生的呢?这时这个类不是还没有完结呢?请教请教 |
14.Re:【请教】关于this的理解!! [Re: prettyxx] | Copy to clipboard |
Posted by: ww1ww1 Posted on: 2004-12-05 10:11 好,今天继续: 既然人是一个Object,那么prettyxx是人这个Object的其中一分子了(请允许我用这样的比喻)。那么我们怎么用程序来描述prettyxx呢。
在这里,我们创建了Human对象的一个实例:prettyxx |
15.Re:【请教】关于this的理解!! [Re: prettyxx] | Copy to clipboard |
Posted by: prettyxx Posted on: 2004-12-06 01:48 在你的这个例子中:this.name = name;依照你前几贴所讲,this在这里是指当前类,而非一个对象。 我所列例子里:comp=new Component(this);这里的this应该为一个对象,而非类。抑或我在这里的理解是错误的 我对this在程序中的作用有些了解,但不明白this到底是个什么东西?在什么时候产生的?是否是说this时而是对象,时而是类? 从你的帖子里,我只是感觉到你要说明:“类是对对象的描述”,但不知道如何来解释我的几个迷惑之处,还要请教啦^_^ |
16.Re:【请教】关于this的理解!! [Re: prettyxx] | Copy to clipboard |
Posted by: mofeelsc Posted on: 2004-12-06 11:09 我也是新學java不久﹐this 我是這樣理解的: class A{ int flag; public A(){ this(0); } public A(int flag){ this.flag = flag; } } 上面代碼中"this(0)"它其實就是public A(int flag)這個構建器吧﹗我想是因為這兩個構建器一個有參數一個無參數但構建器名字相同﹐如果你想在無參數中使用有參數的構建器你總不能使用A(0);吧﹖所以才會有this這個方法﹐不知理解對否﹖ |
17.Re:【请教】关于this的理解!! [Re: prettyxx] | Copy to clipboard |
Posted by: 九佰 Posted on: 2004-12-06 12:57 我看前面提到几次this是对象,我觉得这么理解有问题。 我认为,this应该是当前类的实例,而不是这个类。 |
18.Re:【请教】关于this的理解!! [Re: 九佰] | Copy to clipboard |
Posted by: gechun0214 Posted on: 2004-12-06 13:12 This 也被称为自身(self)。对当前对象的引用。在类定义中,对当前类的属性和方法的引用是隐式的。this可以使引用变为显式的,从而获得更清晰性,我们一般用法是使用this将当前实例的引用传递给另一个对象。 |
19.Re:【请教】关于this的理解!! [Re: 九佰] | Copy to clipboard |
Posted by: ww1ww1 Posted on: 2004-12-07 00:12 九佰 wrote: 归根结底,prettyxx是没有理解对象,类,实例,这三者之间的关系。 我只是把问题分得简单一些,一步一步的来。初学者有些问题还是比较难搞懂的。其实把我发的贴从头到尾细细的看一边,问题就不难解决了。 |
20.Re:【请教】关于this的理解!! [Re: prettyxx] | Copy to clipboard |
Posted by: snowolf128 Posted on: 2004-12-07 08:39 this指的是当前对象,而不是当前类。 |
21.Re:【请教】关于this的理解!! [Re: ww1ww1] | Copy to clipboard |
Posted by: prettyxx Posted on: 2004-12-07 16:05 针对如下这段代码:
我的理解是:在这里,总共产生了两个对象,分别为Container和Component的对象,其中comp这个引用变量指向了Component的对象,而this指向了Container的对象。 我的疑惑是:这两个对象究竟先产生的是哪个对象? 请教各位了 |
22.Re:【请教】关于this的理解!! [Re: prettyxx] | Copy to clipboard |
Posted by: ww1ww1 Posted on: 2004-12-07 18:08 The object of Container created first, then the Component |
23.Re:【请教】关于this的理解!! [Re: prettyxx] | Copy to clipboard |
Posted by: hxy101015 Posted on: 2005-01-03 16:16 我的理解是 this用来访问隐藏的实例变量 |
24.Re:【请教】关于this的理解!! [Re: prettyxx] | Copy to clipboard |
Posted by: 鸡肋男 Posted on: 2005-01-04 10:18 this就是对象自身,不是类自身,拿上面一个例子: class Container { Component comp;//Component类的变量 public void addComponent(){ comp=new Component(this);//这里的this指的是Container实例本身,以自己作为参数实例化了一个Conponent对象。 } } class Component { Container myContainer; public Component(Container c) { myContainer=c; } } |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |