Chinaunix首页 | 论坛 | 博客
  • 博客访问: 686258
  • 博文数量: 152
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1793
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-12 12:26
个人简介

相信自己,只有不想做的,没有做不到的。

文章分类

全部博文(152)

文章存档

2021年(1)

2015年(2)

2014年(74)

2013年(75)

分类: Java

2014-08-10 09:39:04

目标:掌握利用this返回当前引用的方法

源文件:Account.java
/**
 * 返回当前对象的引用
 * author  guojing;
 * e-mail  guo443193911@126.com
 */


package cn.com.Account;


public class Account {
int accountId = 100000;

public Account creatAccount(){
accountId ++;
return this;
}

public int getAccountid(){
return accountId;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Account count = new Account();

System.out.println("账号是:" + count.creatAccount().getAccountid());

        }
}

由于creatAccount()方法返回了同一个对象,所以可以在这个对象上多次调用方法creatAccount。

编译并运行上面的程序,将得到如下的输出:
账号是:100001


阅读(447) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~