this关键字

作者: 是归人不是过客 | 来源:发表于2020-07-19 15:56 被阅读0次

一、this是什么?
this是一个引用类型,保存在内存的地址
在堆中每一个java对象都有this
this保存内存地址指向自身

举个栗子:

public class p {
    public static void main(String [] args) {
//      创建对象
        MyDate t1 = new MyDate(2008,8,8);
        
        System.out.println(t1.year + "year " + t1.month + "month " + t1.day + "day");
        
        MyDate t2 = new MyDate(2012,12,20);
        
        System.out.println(t2.year + "year " + t2.month + "month " + t2.day + "day");
    }
}
class MyDate{
//  field
    int year;
    int month;
    int day;
    
//  Constructor
    MyDate(){
        
    }
    
    MyDate(int _year,int _month,int _day){
        year = _year;
        month = _month;
        day = _day;
    }
}

来个图:

JVM.png

注意:每个对象的 this 都记录本身对象在堆中所在的地址。

二、this能用在哪些地方?

1、this可以用在成员方法中
2、this可以用在构造方法中

1、用在成员方法中

this用在成员方法中,谁去调用这个成员方法,this就代表谁

this. 可以省略

this可以用来区分成员变量和局部变量

this不能用在静态方法中:
静态方法的执行根本不需要java对象的存在,直接使用 类名. 的方式访问
而this代表的是当前对象。所以静态方法根本没有this

举个栗子:(this用在成员方法中,谁去调用这个成员方法,this就代表谁。

this. 可以省略。)

public class p {
    public static void main(String [] args) {
//      创建对象
        Employee e = new Employee(7369, "HL");
//      工作
        e.work();
        
//      创建对象
        Employee e1 = new Employee(7368, "HF");
//      工作
        e1.work();
    }
}
class Employee{
    int empno;
    String ename;
    
    Employee(){
        
    }
    
    Employee(int _empno,String _ename){
        empno = _empno;
        ename = _ename;
    }
    
//  提供员工方法
    public void work() {
//      System.out.println(ename + " 在工作 ");
        System.out.println(this.ename + " 在工作 ");
    }
//  this用在成员方法中,谁去调用这个成员方法,this就代表谁
//  this. 可以省略
}

再举个栗子(this可以用来区分成员变量和局部变量):

public class p {
    public static void main(String [] args) {
        Manager m1 = new Manager("HL");
        
        Manager m2 = new Manager();  
        m2.setName("HF");
        
        System.out.println(m1.getName());
        System.out.println(m2.getName());
    }
    
}
class Manager{
    private String name; // 成员变量
    
    Manager(){};
    
    Manager(String name){
        this.name = name;
    }
    
    //成员方法
    public void setName(String name) {
        this.name = name;  // this.name 确定成员变量 
    }
    
    public String getName() {
        return name;
//      return this.name;
    }
}

栗子:(this不能用在静态方法中)

public class p {
    public static void main(String [] args) {
        Person.m1();
    }
}

class Person{
    String name;
    
    Person(){};
    
    Person(String name){
        this.name  = name;
    };
    
//  静态方法
    public static void m1() {
//      System.out.println(name); 
//     报错:静态上下文中,无法访问非静态变量
        Person p1 = new Person("HL");
        System.out.println(p1.name);
    }
}

2、this可以用在构造方法中

语法: this(实参);
通过一个构造方法调用另一个构造方法
目的:代码重用
注意:this(实参)必须出现在构造方法的第一行

举个栗子:
需求: 在创建日期对象时,默认为 1970-1-1

public class p {
    public static void main(String [] args) {
//      创建对象
        MyDate t1 = new MyDate(2008,8,8);
        
        System.out.println(t1.year + "year " + t1.month + "month " + t1.day + "day");
        
        MyDate t3 = new MyDate();
        
        System.out.println(t3.year + "year " + t3.month + "month " + t3.day + "day");
        
    }
}
class MyDate{
//  field
    int year;
    int month;
    int day;
    
//  Constructor
    MyDate(){
        this(1970,1,1);
//        this.year = 1971;
//        this.month = 1;
//        this.day = 1;
    }
    
    MyDate(int _year,int _month,int _day){
        year = _year;
        month = _month;
        day = _day;
    }
}
f

相关文章

网友评论

    本文标题:this关键字

    本文链接:https://www.haomeiwen.com/subject/osalkktx.html