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分的不爱结贴

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

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法