• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
superStar1213
博客园    首页    新随笔    联系   管理    订阅  订阅
this的使用

1.this表示的是new出来的对象

2.在编译时:this表示当前类的对象

   在运行时:表示运行类的对象

代码:

package thisTest;
/**
 * 
 * this的使用方法
 *
 */
public class Demo_01 {
    
    public  void init(){
        System.out.println("3 init");
        this.demo();
    }
    
    public  void demo(){
        System.out.println("4 demo");
    }
    public static void main(String[] args) {
        Demo_01 demo = new Demo_01();
        demo.init();
    }

}
package thisTest;
/**
 * 
 * this的使用方法
 * this指的是new 的那个对象
 * 
 *
 */
public class Demo_02 extends Demo_01{
    
    public  void init(){
        super.init();
        System.out.println("1 init");
        this.demo();
    }
    
    public  void demo(){
        System.out.println("2 demo");
    }
    public static void main(String[] args) {
        Demo_02 demo = new Demo_02();
        demo.init();
    }

}

运行结果:

 

posted on 2017-12-27 08:58  superStar1213  阅读(118)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3