import java.util.Scanner;
public class Bank implements Runnable {
Acount acount;
public Bank(Acount a) -->这个构造方法的含义?
{
this.acount=a;
}
public void run()
{
Scanner input=new Scanner(System.in);
System.out.println("请输入你的存款:");
int temp=input.nextInt();
acount.setMoney(temp);
}
}
import java.util.Scanner;
public class Customer implements Runnable {
Acount Acount;
public Customer(Acount Acount){ -->这个构造方法的含义?
this.Acount=Acount;
}
public void run()
{
System.out.println("请输入你的取款金额:");
Scanner input=new Scanner(System.in);
int temp=input.nextInt();
Acount.getMoney(temp);
}
}
public class Acount {
private int money;
public Acount(int money){
this.money=money;
}
Bank b=new Bank(this); -->this指的是什么?
public synchronized void getMoney(int money)
{
while(this.money<money)
{
System.out.println("取款:"+money+" 余额:"+this.money+" 余额不足,正在等待存款......");
//当余额不足时 取款线程处于等待状态
try {
Thread t2=new Thread(b);
t2.start();
wait();
} catch (Exception e) {
// TODO: handle exception
}
}
this.money=this.money-money;
System.out.println("取出:"+money+" 还剩余:"+this.money);
}
public synchronized void setMoney(int money)
{
this.money=this.money+money;
System.out.println("新存入:"+money+" 共计:"+this.money);
//将取款线程唤醒
notify();
}
public static void main(String args[])
{
Acount Acount=new Acount(0);
Customer c=new Customer(Acount);
new Thread(c).start();
}
}
你的问题是构造函数的参数问题
面向对象编程中,学会通过构造函数传参,以达到初始化相关变量为目的
前两个Bank(Acount a)意思就是传一个Acount对象来构造对象
从接下来的一行this.Acount=Acount;就能看出,为了初始化本类变量(对象)
Bank b=new Bank(this);
这个this是什么的判定方法准则就是看作用域
this当前处于一个class的类内
所以,指的是当前实例对象//public class Acount{...}
当你实例化Acount的时候,这个this就会诞生
比如Acount myAcount = new Acount(100);
希望对你有帮助,如问题解决,烦请结贴,如有疑问请追问。
//0分贴子太多了,没人回答就是因为0分的不爱结贴