1 String zhanghu;//定义账户 2 double cunkuanyue;//定义存款余额 3 Account(String zhanghu,double cunkuanyue)//初始化账户,存款余额 4 { 5 //this. 代表当前类 6 this.zhanghu=zhanghu; 7 this.cunkuanyue=cunkuanyue; 8 } 9 //存款 10 //有返回值带参数的方法 11 double cunkuan(double qianshu) 12 { 13 // 验证 14 if (qianshu < 1) 15 { 16 System.out.println("输入错误"); 17 18 } 19 else 20 { 21 System.out.println("存了" + qianshu); 22 this.cunkuanyue += qianshu; 23 } 24 return this.cunkuanyue; 25 } 26 27 28 29 //取款 30 //有返回值带参数的方法 31 double qukuan(double qianshu) { 32 if (qianshu > this.cunkuanyue) { 33 System.out.println("余额不足"); 34 35 } else { 36 System.out.println("取了" + qianshu); 37 this.cunkuanyue -= qianshu; 38 } 39 return this.cunkuanyue; 40 } 41 //查询余额 42 //有返回值无参数的方法 43 double getyue() 44 { 45 return this.cunkuanyue; 46 } 47 48 49 public static void main(String[] args) { 50 // TODO 自动生成的方法存根 51 Account account=new Account("123456",1000); 52 account.cunkuan(100); 53 System.out.println("查询余额"+account.getyue()); 54 account.qukuan(300); 55 System.out.println("查询余额"+account.getyue()); 56 }