A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 百里青山 高级黑马   /  2016-4-11 12:36  /  585 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

This在JAVA中所代表的意思是当前类中的意思. 明确引用的是本类中的属性.
1. 在构造方法或是set方法中初始化类中的属性.
class AA{   
   String name;   
   public AA(String name){   
       this.name=name; //如果不写this那肯定出错编译都通不过.  
   }   
}


2. this用在构造方法中,调用本类中的构造方法.
class AA {   
    String name;   
        
    public AA(){   
    System.out.println("1无参构造.....");   
    }   
        
    public AA(String name){   
        this();   
        this.name=name;   
        System.out.println("2有参构造....."+name);   
    }   
        
    public static void main(String[] args) {   
        AA a = new AA("author");   
    }   
}


3. 代表调用对象本身.
class AA {   
    String name;   
        
    public AA(){   
        System.out.println("1无参构造.....");   
        System.out.println(this); // 打印的是对象a的内存地址.  
    }   
        
    public void play(){   
        System.out.println("游戏人生........");   
    }   
        
    public static void main(String[] args) {   
      AA a = new AA();   
    }   
}


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马