this关键字

发布于2020-08-11   486 次阅读


本文最后更新于2022年6月1日,已超过 1 年没更新!内容可能已失效,请自行测试。

this关键字可以算是Java里面比较复杂的关键字,因为this的使用形式上决定了它的灵活性.程序中使用this实现三类结构

  • 当前类中的属性
  • 当前类中的方法
  • 描述当前对象

this调用本类属性

class Personthis {
  private String name;
  private int age;

  public Personthis(String name, int age) {
    this.name = name;
    this.age = age;
  }

  public void tell() {
    System.out.println("姓名" + name + "年龄" + age);
  }
}

public class ThisDemo {
  public static void main(String[] args) {
    Personthis per = new Personthis("张三", 20);
    per.tell();
  }
}

this调用本类方法

构造方法调用

class PersonThis3 {
  private String name;
  private int age;

  public void tell() {
    System.out.println(name + age);
  }

  public PersonThis3() {
    System.out.println("****初始化成功****");
  }
  ;

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

  public PersonThis3(String name, int age) {
    this(name);
    this.age = age;
  }
  // get,set忽略
}

public class ThisDemo3 {
  public static void main(String[] args) {
    PersonThis3 per = new PersonThis3("张三");
    per.tell();
  }
}

普通方法调用

class Personthis2 {
  private String name;
  private int age;

  public Personthis2(String name, int age) {
    this.setName(name);
    setAge(age); // 加与不加都代表本类方法
  }

  public void tell() {
    System.out.println("姓名" + name + "年龄" + age);
  }

  public void setName(String name) {
    this.name = name;
  }

  public void setAge(int age) {
    this.age = age;
  }

  public String getName() {
    return this.name;
  }

  public int getAge() {
    return this.age;
  }
}

public class ThisDemo2 {
  public static void main(String[] args) {
    Personthis2 per = new Personthis2("张三", 20);
    per.tell();
  }
}

案例代码-构造方法互相调用

/*定义一个描述员工信息的程序类
该类中提供:编号、姓名、部门、工资
在这个类中提供四个构造方法
 无参构造 编号定义为1000,姓名定义为无名氏
 单参构造 传递编号,姓名定义为新员工,部门定义为未定,工资为0
 三参构造 传递编号、姓名、部门,工资为2500
 四参构造 所有属性全部进行传递
 */
class Emp {
  private long empno; // 员工编号
  private String name; // 员工姓名
  private String dept; // 员工部门
  private double salary; // 员工工资

  public Emp() {
    this(1000, "无名氏", null, 0.0);
  }

  public Emp(long empno) {
    this(empno, "新员工", "未定", 0.0);
  }

  public Emp(long empno, String name, String dept) {
    this(empno, name, dept, 0.0);
  }

  public Emp(long empno, String name, String dept, double salary) {
    this.empno = empno;
    this.name = name;
    this.dept = dept;
    this.salary = salary;
  }

  public String getInfo() {
    return "雇员编号: "
        + this.empno
        + " 雇员姓名: "
        + this.name
        + " 所在部门: "
        + this.dept
        + " 基本工资: "
        + this.salary;
  }
}

public class Demo {
  public static void main(String[] args) {
    Emp emp = new Emp(7369L, "斯密斯", "财务部");
    System.out.println(emp.getInfo());
  }
}

综合实战:简单Java类

简单Java类核心的开发结构如下:

  • 类名称一定要有意义,可以明确的描述某一类事物
  • 类之中的所有属性都必须使用private进行封装,同时封装后的属性必须提供set,get方法
  • 类之中可以提供无数多的构造方法,但是必须保留无参的构造方法
  • 类之中不允许出现任何的输出语句,所有内容的获取必须返回
  • [非必须]可提供一个获取对象详细新的方法,暂时将此方法名称定义为getInfo

案例代码-简单Java类

class Dept {
  private long deptno;
  private String dname;
  private String loc;

  public Dept() {}
  ;

  public Dept(long deptno, String dname, String loc) {
    this.deptno = deptno;
    this.dname = dname;
    this.loc = loc;
  }

  public String getInfo() {
    return "部门编号: " + deptno + " 部门名称: " + dname + " 部门位置: " + loc;
  }

  public void setDeptno(long deptno) {
    this.deptno = deptno;
  }

  public void setDname(String dname) {
    this.dname = dname;
  }

  public void setLoc(String loc) {
    this.loc = loc;
  }

  public long getDeptno() {
    return deptno;
  }

  public String getDname() {
    return dname;
  }

  public String getLoc() {
    return loc;
  }
}

public class SimpleDemo {
  public static void main(String[] args) {
    Dept dept = new Dept(10, "技术部", "北京");
    System.out.println(dept.getInfo());
  }
}

=

一沙一世界,一花一天堂。君掌盛无边,刹那成永恒。