this关键字介绍:
this关键字的使用:
1.this可以用来修饰或者调用:属性、方法、构造器
2.this修饰属性和方法:理解为:当前对象。当修饰构造器的时候:理解为当前正在创造的对象
在类的方法中,我们可以使用“this.属性”或者“this.方法”的方式,调用当前对象属性或方法。但是通常情况下我们都选择省略“this.”。特殊的:如果方法的形参和类的属性同名时,必须使用this关键字(this.变量),表明此变量是属性,而非形参。
在类的构造器中,我们可以使用“this.形参”的方式,调用当前正在创造的对象的构造器。
this可以在方法里面调方法,构造器里面调构造器this()
- 我们在类的构造器中,可以显示的使用“this(形参列表)”方式,调用本类中指定的其他构造器
- 构造器中不能通过“this(形参列表)”方式调用自己
- 如果一个类中有n个构造器,则最多有n-1构造器中使用了“this(形参列表)”
- 规定:“this(形参列表)”必须声明在当前构造器的首行
- 构造器内部,最多只能声明一个“this(形参列表)”,用来调用其他构造器
package com.myobjectoriented01.nonworking;
public class Person10 {
private String name;
private int age;
public Person10() {}
public Person10(int age) {
this.age=age;
}
public Person10(String name,int age) {
this(age);
this.name=name;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
package com.myobjectoriented01.nonworking;
public class Person10Text {
public static void main(String[] args) {
Person10 p1=new Person10("lily",20);
System.out.println(p1.getAge()+p1.getName());
}
}
题目一:
Boy类的代码:
package com.myobjectoriented.nonworking;
/**
*
* @Desciption
* @author Abraham
* @email 1290807550@qq.com
* @version JDK1.8
* @date 2021年3月24日下午4:48:33
*/
public class Boy {
private String name;
private int age;
public Boy(String name,int age) {
this.name=name;
this.age=age;
}
public void setName(String name) {
this.name=name;
}
public String getName() {
return this.name;
}
public void setAge(int age) {
this.age=age;
}
public int getAge() {
return this.age;
}
public void marry(Girl girl) {
System.out.println("我想娶"+girl.getName());
}
public void shout(int age) {
if(this.age>22) {
System.out.println("我想结婚!");
}
else {System.out.println("年纪不够,再去打打酱油");}
}
}
Girl类的代码:
package com.myobjectoriented.nonworking;
/**
*
* @Desciption
* @author Abraham
* @email 1290807550@qq.com
* @version JDK1.8
* @date 2021年3月24日下午4:48:39
*/
public class Girl {
private String name;
private int age;
public Girl(String name,int age) {
this.name=name;
this.age=age;
}
public void setName(String name) {
this.name=name;
}
public String getName() {
return this.name;
}
public void marry(Boy boy) {
System.out.println("我想嫁给"+boy.getName());
boy.marry(this);
}
public void compare(Girl girl) {
if(this.age>girl.age) {
System.out.println("我大");
}else if(this.age<girl.age){ System.out.println("她大");}
else System.out.println("一样大");
}
}
测试类的代码:
package com.myobjectoriented.nonworking;
/**
*
* @Desciption
* @author Abraham
* @email 1290807550@qq.com
* @version JDK1.8
* @date 2021年3月24日下午5:20:04
*/
public class BoyGirlText {
public static void main(String[] args) {
Boy boy=new Boy("梁山伯",25);
boy.shout(25);
System.out.println(boy.getAge());
Girl girl=new Girl("祝英台",23);
//boy.marry(girl);
girl.marry(boy);
Girl girl01=new Girl("苍老师",23);
girl.compare(girl01);
}
}
银行账户练习题目:
先造一个银行账户的类
package com.myobjectoriented.nonworking;
/**
*
* @Desciption
* @author Abraham
* @email 1290807550@qq.com
* @version JDK1.8
* @date 2021年3月24日下午5:48:36
*/
public class BankAccount {
private int ID;//账户名
private double balance;//余额
private double annualInterestRate;//年利率
public BankAccount(int ID,double balance,double annualInterestRate) {//构造器:直接传递客户信息
this.ID=ID;
this.balance=balance;
this.annualInterestRate=annualInterestRate;
}
public int getID() {
return this.ID;
}
public double getbalance() {
return this.balance;
}
public double getannualInterestRate() {
return this.annualInterestRate;
}
public void setID(int ID) {
this.ID=ID;
}
public void setbalance(double balance) {
this.balance=balance;
}
public void setannualInterestRate(double annualInterestRate) {
this.annualInterestRate=annualInterestRate;
}
public void withdraw(double amount) {//取钱
if(amount>balance) {
System.out.println("余额不足,取款失败!");
return;
}
this.balance-=amount;
System.out.println("成功取出"+amount+"元");
}
public void deposit(double amount) {//存钱
this.balance+=amount;
System.out.println("成功存入"+amount+"元");
}
public void printBankAccountMoney(){
System.out.println("当前账户余额为:"+balance);
}
}
然后造一个客户的类
package com.myobjectoriented.nonworking;
/**
*
* @Desciption
* @author Abraham
* @email 1290807550@qq.com
* @version JDK1.8
* @date 2021年3月24日下午7:37:29
*/
public class Customer {
private String firstName;
private String lastName;
private BankAccount account;
public Customer(String f,String l) {
firstName=f;
lastName=l;
}
public String getFirstname() {
return this.firstName;
}
public String getLastname() {
return this.lastName;
}
public BankAccount getBankAccount() {
return account;
}
public void setBankAccount(BankAccount account) {
this.account=account;
}
public void customerinfomation() {
System.out.println("客户名:"+firstName+" "+lastName+" "+"账户名:"+account.getID()+" "+"账户余额:"+account.getbalance()+" "+"当前利率:"+account.getannualInterestRate());
}
}
最后造一个案例类:
package com.myobjectoriented.nonworking;
public class Customertext {
public static void main(String[] args) {
Customer customer=new Customer("Jane","Smith");
BankAccount accountJane=new BankAccount(1000,2000,0.0123);
//下面三行是错误写法,不需要这么麻烦
//customer.getBankAccount().setID(1000);
//customer.getBankAccount().setbalance(2000);
//customer.getBankAccount().setannualInterestRate(0.0123);
customer.setBankAccount(accountJane);
customer.getBankAccount().deposit(100);
customer.getBankAccount().withdraw(960);
customer.getBankAccount().withdraw(2000);
customer.customerinfomation();
}
}
package com.myobjectoriented11.nonworking;
public class Account {
private double balance;//余额
public Account(double init_balance) {//初始化余额
this.balance=init_balance;
}
public double getBalance() {
return this.balance;
}
public void deposit(double money) {
if(money>0) {
this.balance+=money;
System.out.println("成功存入款项"+money+"元,"+"当前账户余额为:"+this.balance+"元。");
}
else {System.out.println("输入有误,请重新输入!");}
}
public void withdraw(double money) {
if(money>balance) {System.out.println("您的余额不足,取款失败!");
}
else{
this.balance-=money;
System.out.println("成功取出款项"+money+"元,"+"当前账户余额为:"+this.balance+"元。");
}
}
}
package com.myobjectoriented11.nonworking;
public class Customer {
private String firstName;
private String lastName;
private Account account;
public Customer(String f,String l) {
this.firstName=f;
this.lastName=l;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public Account getAccount() {
return this.account;
}
public void setAccount(Account account) {
this.account=account;
System.out.println(account.getBalance());
}
}
package com.myobjectoriented11.nonworking;
public class Bank {
//private Customer[] customers=new Customer[600000];
private Customer[] customers;
private int numberOfCustomer;
public Bank() {
customers=new Customer[10];
}
public void addCustomer(String f,String l) {
Customer cust=new Customer( f,l);
// customers[] =cust; 错误写法,多思考问题所在,有时候我们不是缺少知识,乃是缺少亮光
customers[numberOfCustomer]=cust;
numberOfCustomer++;
}
public int getNumOfCustomers() {
return this.numberOfCustomer;
}
public Customer getCustomer(int index) {// 不要写成了public Customer[] getCustomer(int index), 不要加[]
if(index>=0&&index<numberOfCustomer) {
return customers[index];
}
return null;
}
}
package com.myobjectoriented11.nonworking;
public class BankCustomerText {
public static void main(String[] args) {
Bank bank=new Bank();
bank.addCustomer("abraham", "goldman");
bank.getCustomer(0).setAccount(new Account(5000));
bank.getCustomer(0).getAccount().withdraw(500);
}
}