java_面向对象入门(二)----封装(建立一个银行账户类(acount))

封装:1、类的封装是指将对象的状态信息隐藏在对象内部,不允许外部程序直接访问对象的内部信息,而是通过该类提供的方法实现对内部信息的访问;
2、在定义一个类时,将类的属性私有化,即使用private关键字进行修饰类的属性。

1、编写程序,模拟银行账户存取款与转账来实现类的封装。
2、``本篇建立一个银行账户类(acount),每个账户包括账号、姓名、账户余额等三个属性,银行账户类具有建立新帐号、查询余额、存款、取款、转账(即从本账户把钱转给另一个账户)等功能。

public class account {
	private int id;//账号
    private String name;//姓名
    private double balance;//余额
    public account(int id, String name, double balance) {
    	this.id = id;
    	this.name = name;
    	this.balance = balance;
    	}
         public int getId() {
   	         return id;
   	     }
         public void setId(int id) {
        	 this.id = id;
         }
         public String getName() {
       	     return name;
         }
         public void setName(String name){
    	     this.name = name;
    	 }
         public double getBalance() {
        	 return balance;
         }
         public void setBalance(double balance) {
        	 this.balance = balance;
         }//get,set方法对私有属性提供公有方法,实现封装
     
         //账户信息
         public void information() {
        	 System.out.println("账户: " + id);
        	 System.out.println("姓名: " + name);
        	 System.out.println("余额: " + balance);
        	 }
         //取款
         public void withDraw(double money) {
        	  if (money > this.balance)
        	     System.out.println("你的余额不足");
        	     else
        	     this.balance = (this.balance - money);
        	     System.out.println("账户: "+ this.name + " 你的余额为: " + this.balance);
             }
         //存款
         public void deposit(double money) {
        	   this.balance = (this.balance + money);
        	   System.out.println("账户: "+ this.name + " 你的余额为: " + this.balance);
        	  }
         //转账
         public void transfer(account other,double money){
        	 if(this.balance<money) {
        		 System.out.println("您的余额不足,不能转账");
        	 }
        	 this.balance = (this.balance - money);
        	 other.balance = other.balance + money;
        	 System.out.println("账户: "+ this.name +" 你的余额为: " + this.balance);
        	 	
         }
  }

创建Bank类对上述方法进行测试
测试内容如下:
具体如下:
创建(“001”,“张三”,1000)、(“002”,“李四”,300)两个账号,给两个账号各存入500元,张三取出200元,张三转给李四300元,最后显示2人余额。

public class Bank {

	public static void main(String[] args) {
		account zhangsan = new account(001,"张三",1000);//张三开户
		account lisi = new account(002,"李四",300);//李四开户
		zhangsan.deposit(500);
		lisi.deposit(500);
		zhangsan.withDraw(200);
		zhangsan.transfer(lisi,300);
		zhangsan.information();
		lisi.information();

	}

}

运行结果如下:

账户: 张三 你的余额为: 1500.0
账户: 李四 你的余额为: 800.0
账户: 张三 你的余额为: 1300.0
账户: 张三 你的余额为: 1000.0
账户: 1
姓名: 张三
余额: 1000.0
账户: 2
姓名: 李四
余额: 1100.0

  • 0
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,这是一个多线程的问题。下面是一个简单的 Java 代码实现: ```java public class BankAccount { private double balance; public BankAccount(double initialBalance) { this.balance = initialBalance; } public synchronized void deposit(double amount) { System.out.println("Depositing " + amount + "..."); this.balance += amount; System.out.println("New balance is " + this.balance + "."); } public static void main(String[] args) { BankAccount account = new BankAccount(0); Thread thread1 = new Thread(() -> { for (int i = 0; i < 3; i++) { account.deposit(1000); } }); Thread thread2 = new Thread(() -> { for (int i = 0; i < 3; i++) { account.deposit(1000); } }); thread1.start(); thread2.start(); try { thread1.join(); thread2.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Final balance is " + account.balance + "."); } } ``` 在这个例子中,我们创建了一个 `BankAccount` ,其中包含一个 `deposit` 方法用于存钱。由于多个线程会同时访问 `BankAccount` 对象,因此我们在 `deposit` 方法上使用了 `synchronized` 关键字来进行同步控制,确保线程安全。 然后,我们创建了两个线程,分别代表两个存款操作,每个操作都会存入 1000 元,执行 3 次。我们使用 Lambda 表达式来定义线程的行为。在线程的 `run` 方法中,我们调用 `deposit` 方法来存钱。 最后,我们启动两个线程,等待它们完成,然后输出最终的账户余额。 需要注意的是,由于使用了同步控制,这个程序可以保证线程安全,确保最终的账户余额是正确的。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值