From e01de6c08a7d16ed1549421215979a60095ee685 Mon Sep 17 00:00:00 2001 From: LeiJuntao <2606542098@qq.com> Date: Sun, 22 Mar 2026 20:29:59 +0800 Subject: [PATCH] =?UTF-8?q?w3=E5=B0=81=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- W3/封装 .java | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 W3/封装 .java diff --git a/W3/封装 .java b/W3/封装 .java new file mode 100644 index 0000000..e46dc1c --- /dev/null +++ b/W3/封装 .java @@ -0,0 +1,45 @@ +public class BankAccount { + private String accountNumber; + private String ownerName; + private double balance; + + public BankAccount(String accountNumber, String ownerName) { + this.accountNumber = accountNumber; + this.ownerName = ownerName; + this.balance = 0.0; + } + + public String getAccountNumber() { + return this.accountNumber; + } + + public String getOwnerName() { + return this.ownerName; + } + + public void setOwnerName(String ownerName) { + this.ownerName = ownerName; + } + + public double getBalance() { + return this.balance; + } + + public void deposit(double amount) { + if (amount > 0) { + this.balance += amount; + System.out.println("存款成功,存入金额:" + amount); + } else { + System.out.println("存款失败:金额必须大于0"); + } + } + + public void withdraw(double amount) { + if (amount > 0 && amount <= this.balance) { + this.balance -= amount; + System.out.println("取款成功,取出金额:" + amount); + } else { + System.out.println("取款失败:金额必须大于0且不超过当前余额"); + } + } +}