首页 > 其他 > 详细

this

时间:2020-02-23 17:32:34      阅读:61      评论:0      收藏:0      [点我收藏+]

普通方法中,this总是指向调用该方法的对象;
构造方法中,this总是指向正要初始化的对象;
static方法不能使用this;

package mypro01;

public class Teacher {
    
    
    String name;
    int id;
    
    //构造方法中,this总是指向正要初始化的对象;
    
    public Teacher(String name) {
        this.name = name;
        System.out.println(this.name+" is a teacher");
    }
    
    public void setName(String name) {
        this.name=name;
    }
    
    //普通方法中,this总是指向调用该方法的对象;
    public void teach() {
        System.out.println(this.name+" is teaching");
    }
    
    public static void main(String[] args) {
        Teacher t = new Teacher("wang");//wang is a teacher
        t.setName("qiao");
        t.teach();  //qiao is teaching
    }  
      
  }

 

this

原文:https://www.cnblogs.com/hapyygril/p/12350606.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!