用java设计一个银行账户类,该类具有以下属性和方法:账户号码、账户类型、余额、存款、取款。其中存款和取款方法会改变余额
时间: 2023-07-10 19:24:56 浏览: 178
好的,下面是Java版本的银行账户类设计:
```java
public class BankAccount {
private int accountNumber;
private String accountType;
private double balance;
public BankAccount(int accountNumber, String accountType, double balance) {
this.accountNumber = accountNumber;
this.accountType = accountType;
this.balance = balance;
}
public void deposit(double amount) {
this.balance += amount;
}
public void withdraw(double amount) {
if (amount > this.balance) {
System.out.println("Not enough funds!");
} else {
this.balance -= amount;
}
}
// Getters and setters
public int getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(int accountNumber) {
this.accountNumber = accountNumber;
}
public String getAccountType() {
return accountType;
}
public void setAccountType(String accountType) {
this.accountType = accountType;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
```
这个类有三个属性:账户号码、账户类型和余额。它还有两个方法:存款和取款。存款方法将给定的金额添加到余额中,而取款方法将从余额中减去给定的金额。在取款方法中,如果请求的金额大于余额,它会打印一个“Not enough funds!”的错误信息。
此外,这个类还有三个getter和setter方法,用于访问和修改属性。
阅读全文