用Java编写程序:模拟银行账户操作。 要求:定义一个银行账户类,包含属性:账号、姓名、存款, 方法:存款、取款、查询余额,根据类的合理性原则设计其他方法,最后创建对象对其进行测试。
时间: 2023-05-20 16:06:28 浏览: 215
好的,这是一个很好的编程问题。以下是一个可能的解决方案:
```java
public class BankAccount {
private String accountNumber;
private String name;
private double balance;
public BankAccount(String accountNumber, String name, double balance) {
this.accountNumber = accountNumber;
this.name = name;
this.balance = balance;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
} else {
System.out.println("Insufficient funds");
}
}
public double getBalance() {
return balance;
}
// Other methods can be added here, such as transferring funds to another account
public static void main(String[] args) {
BankAccount account = new BankAccount("123456789", "John Smith", 1000.0);
account.deposit(500.0);
account.withdraw(200.0);
System.out.println("Account balance: " + account.getBalance());
}
}
```
这个类包含了账号、姓名和存款三个属性,以及存款、取款和查询余额三个方法。在创建对象后,可以使用这些方法对账户进行操作,并测试它们是否按预期工作。
阅读全文
相关推荐

















