博客统计信息

51cto推荐博客
用户名:vfast_chenxy
文章数:178
评论数:67
访问量:105032
无忧币:885
博客积分:1586
博客等级:6
注册日期:2009-04-08

Java this的使用
2012-02-06 17:13:51

this是指当前对象自己。
当在一个类中要明确指出使用对象自己的的变量或函数时就应该加上this引用如下面这个例子中:

public class A {

    String s = "Hello";

    public A(String s) {
        System.out.println("s = " + s);
        System.out.println("1 -> this.s = " + this.s);
        this.s = s;
        System.out.println("2 -> this.s = " + this.s);
    }

    public static void main(String[] args) {
        new A("HelloWorld!");
    }
}

运行结果
s = HelloWorld!
1 -> this.s = Hello
2 -> this.s = HelloWorld!

    这个例子中,构造函数A中参数s与类A的变量s同名,这时如果直接对s进行操作则是对参数s进行操作。若要对类A的变量s进行操作就应该用this进行引用。行结果的一行就是直接对参数s进行打印结果;后面两行分别是对对象A的变量s进行操作前后的打印结果。

================================================================

this()在构造函数中使用

this.x调用成员变量

 

分享至
更多
一键收藏,随时查看,分享好友!
0人
了这篇文章
类别:linux应用技术圈()┆阅读()┆评论() ┆ 推送到技术圈返回首页

文章评论

 
 

发表评论            

【技术门诊】专家解析:软考重点难点及应试技巧
昵  称:
登录  快速注册
验证码:

请点击后输入验证码博客过2级,无需填写验证码

内  容: