java定义一个account的类
时间: 2023-10-02 16:10:14 浏览: 133
以下是一个简单的Java代码来定义一个account类:
```
public class Account {
// 定义账户属性
private String accountNumber;
private String accountType;
private double balance;
// 构造方法
public Account(String accountNumber, String accountType, double balance) {
this.accountNumber = accountNumber;
this.accountType = accountType;
this.balance = balance;
}
// 获取账户余额
public double getBalance() {
return balance;
}
// 存款
public void deposit(double amount) {
balance += amount;
}
// 取款
public void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
} else {
System.out.println("余额不足");
}
}
// 转账
public void transfer(Account recipient, double amount) {
if (balance >= amount) {
balance -= amount;
recipient.deposit(amount);
} else {
System.out.println("余额不足");
}
}
}
```
这个类包含了账户的属性和一些基本操作,比如存款、取款、转账等。通过这个类,我们可以创建多个账户对象,并对它们进行操作。
阅读全文