-
-
Save vaughan0/4011973 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class BankAccount { | |
public static void main(String[] args){ | |
Scanner in = new Scanner(System.in); | |
BankAccount account = new BankAccount(1000); | |
account.deposit(500); | |
account.withdraw(50); | |
System.out.println("BankAccount " + account.getNumber()); | |
System.out.println("Has a balance of " + account.getBalance()); | |
} | |
// These are different for each account | |
private double balance; | |
private int accountNumber; | |
// This is shared by all accounts, so it's static | |
private static int lastAccountNumber = 0; | |
// This is a constructor: no return type (void, boolean etc) and has the same name as the class. | |
public BankAccount(double intialBalance) | |
{ | |
balance = intialBalance; | |
accountNumber = lastAccountNumber + 1; | |
lastAccountNumber = accountNumber; | |
} | |
public void deposit(double depositAmount) | |
{ | |
balance += depositAmount; | |
} | |
public boolean withdraw(double withdrawAmount) | |
{ | |
if (withdrawAmount > balance){ | |
System.out.println("Insufficient Funds!!!"); | |
return false; | |
} else { | |
balance -= withdrawAmount; | |
return true; | |
} | |
} | |
public int getNumber() | |
{ | |
return accountNumber; | |
} | |
public double getBalance() | |
{ | |
return balance; | |
} | |
} |
Hi, is there any policy for concurrency issues? What about two threads call the transfer method of the same account instance. Did I miss something?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
请问并发安全是怎么保证的?Account代码中没看到并发措施