class Book {
private String title;
private double price;
public Book(String t, double p) {
title = t;
price = p;
}
//setter、getter略
public String getInfo() {
return "书名:" + title + ",价格:" + price;
}
}
public class MainClass {
public static void main(String[] args) {
Book book = new Book("Java基础入门", 89.8);
System.out.println(book.getInfo());
}
}
观察如下的一点:
public Book(String t, double p) {
title = t;
price = p;
}
class Emp {
private int empno;
private String ename;
private double sal;
private String dept;
public Emp() {
this.empno = 0;
this.ename = "无雇员";
this.sal = 0.0;
this.dept = "未定";
}
public Emp(int empno) {
this.empno = empno;
this.ename = "临时工";
this.sal = 800.0;
this.dept = "后勤";
}
public Emp(int empno, String ename) {
this.empno = empno;
this.ename = ename;
this.sal = 2000.0;
this.dept = "技术部";
}
public Emp(int empno, String ename, double sal, String dept) {
this.empno = empno;
this.ename = ename;
this.sal = sal;
this.dept = dept;
}
//setter、getter略
public String getInfo() {
return "雇员编号:" + this.empno + ",名称:" + this.ename + ",工资:" + this.sal + ",部门:" + this.dept;
}
}
public class MainClass {
public static void main(String[] args) {
Emp ea = new Emp();
Emp eb = new Emp(9527);
Emp ec = new Emp(7566, "ALLEN");
Emp ed = new Emp(7869, "KING", 5000.0, "Boss");
System.out.println(ea.getInfo());
System.out.println(eb.getInfo());
System.out.println(ec.getInfo());
System.out.println(ed.getInfo());
}
}
虽然以上的代码完成了功能,但是却存在有重复的操作,很明显,这种代码不符合实际的开发要求。
**实现方式二:**利用构造方法简化代码
class Emp {
private int empno;
private String ename;
private double sal;
private String dept;
public Emp() {
this(0, "无雇员", 0.0, "未定");
}
public Emp(int empno) {
this(empno, "临时工", 800.0, "后勤");
}
public Emp(int empno, String ename) {
this(empno, ename, 2000.0, "技术部");
}
public Emp(int empno, String ename, double sal, String dept) {
this.empno = empno;
this.ename = ename;
this.sal = sal;
this.dept = dept;
}
//setter、getter略
public String getInfo() {
return "雇员编号:" + this.empno + ",名称:" + this.ename + ",工资:" + this.sal + ",部门:" + this.dept;
}
}
public class MainClass {
public static void main(String[] args) {
Emp ea = new Emp();
Emp eb = new Emp(9527);
Emp ec = new Emp(7566, "ALLEN");
Emp ed = new Emp(7869, "KING", 5000.0, "Boss");
System.out.println(ea.getInfo());
System.out.println(eb.getInfo());
System.out.println(ec.getInfo());
System.out.println(ed.getInfo());
}
}
此时的操作就利用了构造方法间的相互调用解决了代码的重复问题。
表示当前对象
当前对象指的是当前正在调用类中方法的对象。
class Book {
public void print() {
//this就是当前调用方法的对象
System.out.println("this = " + this);
}
}
public class MainClass {
public static void main(String[] args) {
Book booka = new Book();
Book bookb = new Book();
System.out.println("booka = " + booka);
booka.print();
System.out.println("----------------------------");
System.out.println("bookb = " + bookb);
bookb.print();
}
}
之前出现的“this.属性”实际上指的就是当前对象中的属性,一定是保存在堆内存中的内容。
**思考:**观察如下代码的执行
class A {
private B b;
//2、执行A()
public A() {
//3、实例化B类对象
this.b = new B(this); //4、this == temp
this.b.get(); //7、调用b.get()
}
//10、调用print()
public void print() {
System.out.println("Hello World !");
}
}
class B {
private A a;
//5、调用B(A a),a == temp
public B(A a) {
this.a = a; //6、保存a对象(temp)
}
//8、调用this.a(temp).print()
public void get() {
this.a.print();
}
}
public class MainClass {
public static void main(String[] args) {
//1、实例化A类对象,调用A()
A temp = new A();
}
}