this关键字
this可以用于:属性,方法,构造器
this用于属性和方法时,this可以理解为:当前对象
this用于属性和方法
使用this.属性 this.方法
this用于构造器
this(参数列表)调用构造器
只能放在首行
/**
* this关键字
*/
public class Boy {
private String name;
private int age;
public Boy(){
}
public Boy(String name,int age){
this.name=name;
this.age=age;
}
public Boy(String name){
this(name,5);
this.name=name;
}
public void setName(String name){
this.name=name;
}
public void setAge(int age){
this.age=age;
}
public void eat(){
System.out.println("吃饭");
}
public void walk(){
this.eat();
}
}
public class Customer {
private String firstName;
private String lastName;
private Account account;
public Customer(String firstName,String lastName){
this.firstName=firstName;
this.lastName=lastName;
}
public void setAccount(Account account){
this.account=account;
}
public String getFirstName(){
return this.firstName;
}
public String getLastName(){
return this.lastName;
}
public Account getAccount(){
return this.account;
}
public static void main(String[] args) {
Customer customer=new Customer("Jane","Smith");
Account account=new Account(1000,2000,0.0123);
customer.setAccount(account);
customer.getAccount().deposit(100);
customer.getAccount().withdraw(960);
customer.getAccount().withdraw(2000);
}
}