this关键字
this关键字代表了所属方法的调用者对象
this关键字的作用:
- 如果存在同名成员变量与局部变量时,在方法内部默认是访问局部变量的数据,可以通过this关键字指定访问成员变量的数据。
- 在一个构造方法中可以调用另外一个构造方法初始化对象。
this关键字调用其他构造方法要注意的事项:
- this关键字调用其他的构造方法时,this关键字必须位于构造方法中的第一行
- this关键字在构造方法中不能出现循环调用的情况,这回变成死循环。
this关键字要注意的事项:
- 存在同名的成员变量与局部变量时,在方法的内部访问的是局部变量(Java采取的是“就近原则”的机制访问的)。
- 如果在一个方法中访问了一个变量,该变量名只存在于成员变量时,那么Java编译器会在该变量前面添加this关键字。
程序1.1 冗余程序
public class Book {
private String title;
private double price;
public Book(){
System.out.println("一个新的Book对象产生。"); //假设这里有50次输出
}
public Book(String title){
System.out.println("一个新的Book对象产生。"); //假设这里有50次输出
this.title = title;
}
public Book(String title,double price){
System.out.println("一个新的Book对象产生。"); //假设这里有50次输出
this.title = title;
this.price = price;
}
//这里省略get、set方法
public String getInfo(){
return "书名:"+title+", 价格:"+price;
}
}
public class DemoText {
public static void main(String[] args) {
Book book = new Book("Java开发",88.8);
book.getInfo();
}
}
运行结果: 一个新的Book对象产生。 //省略49行代码
书名:Java开发,价格:88.8
可以看出程序中出现了大量的重复代码,那么如何改进呢?
可以通过this关键字实现。
程序1.2 改进程序1.1
通过使用this()调用本类的无参构造方法
//Book类
public class Book {
private String title;
private double price;
public Book(){
System.out.println("一个新的Book对象产生。"); //假设这里有50次输出
}
public Book(String title){
// System.out.println("一个新的Book对象产生。"); //假设这里有50次输出
this();
this.title = title;
}
public Book(String title,double price){
// System.out.println("一个新的Book对象产生。"); //假设这里有50次输出
this();
this.title = title;
this.price = price;
}
//这里省略get、set方法
public String getInfo(){
return "书名:"+title+", 价格:"+price;
}
}
//主类
public class DemoText {
public static void main(String[] args) {
Book book = new Book("Java开发",88.8);
book.getInfo();
}
}
运行结果: 一个新的Book对象产生。 //省略49行代码
书名:Java开发,价格:88.8
注意:关于 this 调用构造的限制。
在使用 this 调用构造方法时,存在两个重要的限制(这些都可以在程序编译时检测出来):
- 使用“this()” 调用构造方法形式的代码只能够放在构造方法的首行;
- 进行构造方法相互调用时,一定要保留调用的出口。
实例: 定义一个雇员类(编号、姓名、工资、部门),在这个类里提供了一下4个构造方法。
- 无参构造:编号为0,姓名为无名氏,工资为0.0,部门设置为“未定”;
- 单参构造(传递编号):姓名为“临时工”,工资为800.0,部门为后勤;
- 双参构造(传递编号、姓名):工资为2000.0,部门为技术部;
- 四参构造。
**实现方法一:**按照传统的方法实现
public class Emp {
private int empid;
private String ename;
private double salary;
private String dept;
public Emp(){ //无参构造
this.empid = 0;
this.ename = "无名氏";
this.salary = 0.0;
this.dept = "未定";
}
public Emp(int empid){ //单参构造
this.empid = empid;
this.ename = "临时工";
this.salary = 800.0;
this.dept = "后勤部";
}
public Emp(int empid,String ename){ //双参构造
this.empid = empid;
this.ename = ename;
this.salary = 2000.0;
this.dept = "技术部";
}
public Emp(int empid,String ename,double salary,String dept){ //四参构造
this.empid = empid;
this.ename = ename;
this.salary = salary;
this.dept = dept;
}
public String getInfo(){
return "雇员编号:"+this.empid+", 姓名:"+this.ename+", 工资:"+this.salary+",部门:"+this.dept;
}
//省略set、get方法
}
//测试类
public class DemoText {
public static void main(String[] args) {
Emp ea = new Emp();
Emp eb = new Emp(6666);
Emp ec = new Emp(6666,"王安");
Emp ed = new Emp(6666,"达纳",6000.0,"财务部");
System.out.println(ea.getInfo());
System.out.println(eb.getInfo());
System.out.println(ec.getInfo());
System.out.println(ed.getInfo());
}
}
运行结果: 雇员编号:0, 姓名:无名氏, 工资:0.0,部门:未定
雇员编号:6666, 姓名:临时工, 工资:800.0,部门:后勤部
雇员编号:6666, 姓名:王安, 工资:2000.0,部门:技术部
雇员编号:6666, 姓名:达纳, 工资:6000.0,部门:财务部
本程序首先分别利用不同的构造方法产生了4个实例化对象,然后分别调用getInfo() 方法进行输出。虽然这个时候已经完成了开发的要求,但是却可以发现程序中存在重复的代码,很明显,这种包含重复代码的程序一定不符合实际的开发要求。
**实现方法二:**利用构造方法互相调用简化代码。
public class Emp {
private int empid;
private String ename;
private double salary;
private String dept;
public Emp(){ //无参构造
this(0,"无名氏",0.0,"未定");
}
public Emp(int empid){ //单参构造
this(empid,"临时工",800.0,"后勤部");
}
public Emp(int empid,String ename){ //双参构造
this(empid,ename,2000.0,"技术部");
}
public Emp(int empid,String ename,double salary,String dept){ //四参构造
this.empid = empid;
this.ename = ename;
this.salary = salary;
this.dept = dept;
}
public String getInfo(){
return "雇员编号:"+this.empid+", 姓名:"+this.ename+", 工资:"+this.salary+",部门:"+this.dept;
}
//省略set、get方法
}
//测试类
public class DemoText {
public static void main(String[] args) {
Emp ea = new Emp();
Emp eb = new Emp(6666);
Emp ec = new Emp(6666,"王安");
Emp ed = new Emp(6666,"达纳",6000.0,"财务部");
System.out.println(ea.getInfo());
System.out.println(eb.getInfo());
System.out.println(ec.getInfo());
System.out.println(ed.getInfo());
}
}
运行结果: 雇员编号:0, 姓名:无名氏, 工资:0.0,部门:未定
雇员编号:6666, 姓名:临时工, 工资:800.0,部门:后勤部
雇员编号:6666, 姓名:王安, 工资:2000.0,部门:技术部
雇员编号:6666, 姓名:达纳, 工资:6000.0,部门:财务部
此时通过 “this()”语法的形式,可以有效解决代码重复的问题。