Topic: this 的问题 麻烦大家帮我看看!谢谢 |
Print this page |
1.this 的问题 麻烦大家帮我看看!谢谢 | Copy to clipboard |
Posted by: 3052401056 Posted on: 2006-07-15 15:12 public class Leaf { int i = 0; Leaf increment() { i++; return this; } void print() { System.out.println("i = " + i); } public static void main(String[] args) { Leaf x = new Leaf(); x.increment().increment().increment().print(); } } JAVA 编程思想 上的一个例子 不明白 x.increment().和x.increment().increment(). 这2个函数return 的this 有什么区别? 谢谢大家帮我看下!! 顺便做下广告25466508 JAVA 学习群 暑假里希望大家一起进步!! |
2.Re:this 的问题 麻烦大家帮我看看!谢谢 [Re: 3052401056] | Copy to clipboard |
Posted by: myohmy Posted on: 2006-07-15 19:11 this指当前对象 Leaf x = new Leaf(); x.increment().increment().increment().print(); 这里可以这么看x.increment(),然后返回了this就相当于返回了当前的对象,对象又可以调用方法, x.increment().increment().increment().print();继续 =this.increment().increment().print();继续=this.increment().print();继续=this.print(); 也就是说每次调用一次increment().都是返回一个this, |
3.Re:this 的问题 麻烦大家帮我看看!谢谢 [Re: 3052401056] | Copy to clipboard |
Posted by: 3052401056 Posted on: 2006-07-15 21:09 谢谢楼上的 呵呵 |
4.Re:this 的问题 麻烦大家帮我看看!谢谢 [Re: 3052401056] | Copy to clipboard |
Posted by: sunjavaduke Posted on: 2006-07-18 12:49 返回的每一个this都是一个相同的对象,而该类成员方法的作用就是改变对象的属性(类的成员变量),对象每调用一次该方法,就改变一次自身的属性,并且把自己做为返回值进行返回。 |
5.Re:this 的问题 麻烦大家帮我看看!谢谢 [Re: 3052401056] | Copy to clipboard |
Posted by: 3052401056 Posted on: 2006-07-18 13:10 谢谢楼上的 呵呵 |
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 |