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

我们创建一个引用类型时,地址存储在栈中,申请出来的空间在堆中。this指针在堆中,指向它本身。代表当前对象的一个引用

构造方法用this

普通方法用this

Public class thisDemo{

  String name;

  int age;

  public thisDemo(String name,int age){

    this.name = name;

    this.age = age;

  }

  public void test1(){

    System.out.println("test1");

  }

  public void test2(String name){

    System.out,println("test2");

    test1();

    //test1();加不加this都可

    System.out,println(name);  //输出lisi

    System.out,println(this.name);  //输出zhangsan

  }

  public void main(String args){

    thisDemo td = new thisDemo("zhangsan", 12);

    System.out.println(td.name);

    System.out.println(td.age);

    test2(“lisi”);

}

posted on 2020-07-24 14:40  长发不及肩  阅读(151)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3