我在构造函数中有一个对象需要传递给另一个类。我正在使用线程,我需要向所有线程共享某个变量。
我需要在构造函数中获取BankExample对象,然后复制它,以便我可以将它传递给run方法中的withdraw方法调用(在事务类中)。
传球看起来有点像这样。
主类-----> runnable类---->行动课。
public class WithdrawThread implements Runnable {
//I take in a thread numer so i can tell which threads run in the terminal.
transaction withdraw = new transaction();
int threadNum;
int balance = 0;
public WithdrawThread(int num, BankExample bal){
threadNum = num;
}
public void run(){
//calls the withdraw method in transaction because i have a single method that takes care of reducing the balance amount.
withdraw.withdraw(threadNum, balance );
}
}
答案 0 :(得分:0)
WithdrawThread(...)
几乎就是你想要的方式。只需创建更相关的另一种方法。