13. this关键字

1.this的概述

  this关键字代表是对象的引用。也就是this在指向一个对象,所指向的对象就是调用该函数的对象引用。

2.this实例,初始化成员变

  class Employee {

    private String name;

    // 提供公有的get set方法

    public String getName() {

    return name;

    }

    public void setName(String n) {

      name = n;

    }

    //构造方法

    public Employee (String name) {

      this.name= name;

    }

  }

3.this调用本类中的构造函数

 

  this()       调用无参的构造方法

  this(name)     调用1个参数的构造方法

 

  注意:

    1.this只能在非静态中(没有static修饰的)函数使用

    2.构造函数间相互调用必须放在构造函数的第一个语句中,否则编译错误

原文地址:https://www.cnblogs.com/zjdbk/p/8877596.html