Java 类和对象
编写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);
结果: