package ltb20180106;
public class TestBankThread {
private int deposit=0;//注意全局变量的摆放.
public TestBankThread() {
}
class BankThread implements Runnable{
public BankThread() {
}
public void setDeposit(int money) {
deposit+=money;
}
public int getMoney(int m) {
deposit=deposit-m;
if(deposit<0) {
System.out.println("ATM余额不足!!!"+deposit);
}
return m;
}
public int getBank() {
return deposit;
}
public void run() {
this.setDeposit(200);
System.out.println("ATM现有存款"+this.getBank());
}
}
class A extends BankThread {
private int a=0;
public A() {
}
public void setA(int i) {
a=a+i;
}
public int getA() {
return a;
}
public void run() {
this.setA(super.getMoney(200));
System.out.println("A从ATM取出:"+this.getA());
System.out.println("ATM现在是"+this.getBank());
this.setA(100);
System.out.println("在存入A100元最后是:"+a);
super.setDeposit(getA());
System.out.println("存入ATM后:"+super.getBank());
}
}
class B extends BankThread {
private int b=0;//余额
private int k=0;//取款数额
public B() {
}
public void setB(int i) {
b=b+i;
}
public int getB(int j) {
b=b-j;
if(j>b) {
System.out.println("类B:余额不足了");
}
k=j;
return j;
}
public int getB() {
return b;
}
public int getK() {
return k;
}
public void run() {
this.setB(super.getMoney(300));
System.out.println("B从ATM取走"+this.getB()+"元");
this.getB(50);
super.setDeposit(this.getB());
System.out.println("B取款"+this.getK()+"元,花掉了.");
System.out.println("ATM当前存款:"+super.getBank());
}
}
public static void main(String[] args) {
TestBankThread tb=new TestBankThread();
Thread t1=new Thread(tb.new BankThread());
t1.start();
Thread t2=new Thread(tb.new A());
t2.start();
Thread t3=new Thread(tb.new B());
t3.start();
}
}