码迷,mamicode.com
首页 > 其他好文 > 详细

133-this知识点

时间:2020-04-21 15:21:20      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:class   知识点   span   col   一点   不能   out   strong   main方法   

1.this代表的是当前对象的引用。通常而言那个对象调用这个方法,这个方法中的this就代表那个对象。

public vide detail(){
    System.out.println(this.nam);//通常this都可以省略  
}

2.为什么构造方法也会有this呢?按理来说此时还没有对象啊?怎么会有this关键字呢?

new Student();当使用new的时候,就会创建出一个对象,然后在往对象的属性中添加值,这样就可以有this了。

   public Student(String name) {
        this.name = name;
    }

3.this关键字不能使用在静态方法中。

    public static void main(String[] args) {
        Student student = new Student("张三");
        System.out.println(this.name);  //这句话是错的,this与我们创建的student一点关系都没有。谁调用这个main方法,这个main方法就是谁。         
    }

 

133-this知识点

标签:class   知识点   span   col   一点   不能   out   strong   main方法   

原文地址:https://www.cnblogs.com/pogusanqian/p/12744265.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!