this关键字

本文介绍了一个简单的Java程序,该程序演示了如何使用链式调用的方法来增加类成员变量的值并打印结果。通过Leaf类的实例展示了如何在构造函数中初始化成员变量,并通过方法返回当前对象实现链式调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package test;
class Leaf{
int i = 0;
//this是指向自身的引用,所以this.i是成员变量,i是形参。
Leaf(int i){this.i = i;}
Leaf increament(){
i++;
return this;
}
void print(){System.out.println("i="+i);}
}
public class Test {
public static void main(String args[]){
Leaf leaf = new Leaf(100);
leaf.increament().increament().print();
}
}


[img]http://dl2.iteye.com/upload/attachment/0116/1298/e09e858a-e06c-3f41-b4f1-26ec811e79a2.jpg[/img]

[img]http://dl2.iteye.com/upload/attachment/0116/1300/22a2a70b-dec9-3d59-a9c0-3bf3927da19d.jpg[/img]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值