編寫Java應用程序。首先定義一個描述銀行賬戶的Account類,包括成員變
量“賬號”和“存款余額”,成員方法有“存款”、“取款”和“余額查詢”。其次,
編寫一個主類,在主類中測試Account類的功能。
類:
1 String bankAccountNumber; 2 String name; 3 double cunKuanYu_e; 4 5 void cunkuan(double cunkuan) { 6 cunKuanYu_e = cunKuanYu_e + cunkuan; 7 System.out.println(name + "你的賬號為:" + bankAccountNumber + "的賬戶中存入" + cunkuan + "元,現有余額" + cunKuanYu_e); 8 } 9 10 void qukuan(double qukuan) { 11 if (cunKuanYu_e >= qukuan) { 12 cunKuanYu_e = cunKuanYu_e - qukuan; 13 System.out.println(name + "你的賬號為:" + bankAccountNumber + "的賬戶中取出" + qukuan + "元,現有余額" + cunKuanYu_e); 14 } else { 15 System.out.println(name + "你的賬號為:" + bankAccountNumber + "的賬戶中余額不足!"); 16 } 17 } 18 19 double getYu_E() { 20 return cunKuanYu_e; 21 }
調用:
1 Account ren1=new Account(); 2 ren1.bankAccountNumber="622848123456789"; 3 ren1.name="張三"; 4 ren1.cunKuanYu_e=587.54; 5 6 7 ren1.qukuan(600.0); 8 System.out.println(ren1.name+"余額="+ren1.getYu_E()); 9 ren1.cunkuan(1000); 10 11 Account ren2=new Account(); 12 ren2.bankAccountNumber="622848987654321"; 13 ren2.name="李四"; 14 ren2.cunKuanYu_e=87.54; 15 16 17 double ren1ye=ren1.getYu_E(); 18 System.out.println(ren1.name+"的賬戶余額:"+ren1ye); 19 double ren2ye=ren2.getYu_E(); 20 System.out.println(ren2.name+"的賬戶余額:"+ren2ye);
結果: