1、设计一个账户类account,包括:(一)成员属性(私有)(1)私有账户标识符id(2)账
时间: 2023-10-13 11:05:03 浏览: 175
户余额balance(3)账户密码password(二)构造方法(1)默认构造方法(2)带参构造方法,初始化账户标识符、账户余额和账户密码(三)成员方法(1)获取账户标识符的方法getAccountId()(2)获取账户余额的方法getBalance()(3)获取账户密码的方法getPassword()(4)设置账户密码的方法setPassword()(5)存款的方法deposit(double money)(6)取款的方法withdraw(double money),注意取款不能超过账户余额,如果超过则抛出异常。
示例代码:
```java
public class Account {
private int id;
private double balance;
private String password;
public Account() {
}
public Account(int id, double balance, String password) {
this.id = id;
this.balance = balance;
this.password = password;
}
public int getAccountId() {
return id;
}
public double getBalance() {
return balance;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void deposit(double money) {
balance += money;
}
public void withdraw(double money) throws Exception {
if (balance < money) {
throw new Exception("余额不足");
} else {
balance -= money;
}
}
}
```
阅读全文