Java03
多线程业务
java
package day08;
public class Account {
private String cardId;//卡号
private double money;//余额
public Account() {
}
public Account(String cardId, double money) {
this.cardId = cardId;
this.money = money;
}
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
public double getMoney() {
return money;
}
@Override
public String toString() {
return "Account{" +
"cardId='" + cardId + '\'' +
", money=" + money +
'}';
}
public void setMoney(double money) {
this.money = money;
}
public void drawMoney(int money) {
//先弄清楚到底是谁来取钱
String name = Thread.currentThread().getName();
if(this.money>=money){
System.out.println(name + "来取钱" + money + "成功!");
this.money=this.money-money;
System.out.println(name + "来取钱后,余额剩余" + this.money);
}else{
System.out.println(name + "来取钱,余额不足~");
}
}
}
java
package day08;
public class DrawThread extends Thread {
private Account account;
public DrawThread(){
//无参构造器
}
public DrawThread(Account account,String name){
super(name);//一定要放在上面
this.account=account;
}
@Override
public void run() {
//线程调用方法进行取钱
account.drawMoney(100000);
}
}