shan915135210 2014-08-09 02:22 采纳率: 42.9%
浏览 1915
已采纳

java引用数据类型的问题,请大神解释下标注的问题.....

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();
 }

}

  • 写回答

1条回答 默认 最新

  • franzhong 2014-08-10 07:56
    关注

    你的问题是构造函数的参数问题
    面向对象编程中,学会通过构造函数传参,以达到初始化相关变量为目的
    前两个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分的不爱结贴

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 怎么在stm32门禁成品上增加记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号