黑马程序员技术交流社区

标题: this关键字总结 [打印本页]

作者: 高廷平    时间: 2012-9-27 21:15
标题: this关键字总结
package mytest_object;
/*
* this 关键字
*
* 1. 使用地方: 构造方法和成员方法
* 2. this代表本类(当前)对象
* 3. 内部类中:  外类.this.属性    指代外部类的属性
* 4. 区分局部和成员变量     当局部和成员变量同名时     成员变量被隐藏  
*    这时可以用    this.成员变量     访问成员变量
* 5. 可以调用本类的构造方法 在无参数的构造方法中调用有参数的
*    this(参数列表)。必须是第一句
*    使用其他重载的完成初始化操作。
*   
*
*
*/
public class MyObjectTest7
{
     public static void main(String[] args)
     {
                Test7 t7=new Test7();
                t7.show();

         }
}

class Test7
{
        int age=10;
        void show()
        {
                int age=100;
                System.out.println(age);//输出100    局部变量
                System.out.println(this.age);//输出10  成员变量
        }

}

作者: 黑幕    时间: 2012-9-27 23:56
正好学习下{:soso_e113:}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2