java account类
时间: 2023-10-02 21:07:09 浏览: 74
Account类是一个Java类,用于表示银行账户。它通常包含以下属性:
- 账户的持有人姓名
- 账户号码
- 账户余额
Account类还可能包含以下方法:
- 存款方法:增加账户余额
- 取款方法:从账户余额中减去指定金额
- 查询余额方法:返回当前账户余额
以下是一个示例的Account类实现:
```
public class Account {
private String holderName;
private int accountNumber;
private double balance;
// Constructor
public Account(String holderName, int accountNumber, double balance) {
this.holderName = holderName;
this.accountNumber = accountNumber;
this.balance = balance;
}
// Deposit method
public void deposit(double amount) {
balance += amount;
}
// Withdraw method
public void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
} else {
System.out.println("Insufficient funds!");
}
}
// Get balance method
public double getBalance() {
return balance;
}
}
```
使用Account类的示例代码:
```
// Create a new account with initial balance of 1000
Account myAccount = new Account("John Smith", 123456, 1000);
// Deposit 500 into the account
myAccount.deposit(500);
// Withdraw 200 from the account
myAccount.withdraw(200);
// Print the current balance
System.out.println("Current balance: " + myAccount.getBalance());
```