//创建一个账户类
class Account{
//账号
private String accountNum;
//账户的余额
private double subsidy;
public Account() {
}
public Account(String accountNum, double subsidy) {
this.accountNum = accountNum;
this.subsidy = subsidy;
}
public String getAccountMe() {
return accountNum;
}
public double getSubsidy() {
return subsidy;
}
public void setAccountMe(String accountMe) {
this.accountNum = accountMe;
}
public void setSubsidy(double subsidy) {
this.subsidy = subsidy;
}
}
/**
* 取款线程
*/
class DrawMoney extends Thread{
//账户对象
private Account account;
//取款金额
private double drawMoney;
public DrawMoney(String name,Account account,double drawMoney){
super(name);
this.account=account;
this.drawMoney=drawMoney;
}
@Override
public void run() {
//判断当前账户余额是否大于或等于取款金额
if(this.account.getSubsidy() >= this.drawMoney){
System.out.println(this.getName()+"取钱成功!吐出钞票:"+this.drawMoney);
try {
Thread.sleep(1000l);
} catch (InterruptedException e) {
e.printStackTrace();
}
//更新账户余额
this.account.setSubsidy(this.account.getSubsidy()-this.drawMoney);
System.out.println("余额为:"+this.account.getSubsidy());
}else {
System.out.println("余额不足,取钱失败");
}
}
}
public class DrawMoneyThread {
public static void main(String[] args) {
Account a=new Account("123456",2000);
DrawMoney dm=new DrawMoney("老婆",a,900);
dm.start();
DrawMoney dm1=new DrawMoney("老公",a,1500);
dm1.start();
}
}
//运行截图