1 package com.hanqi.z1p1; 2 3 public class Account { 4 5 String Bnumber; 6 double YuE; 7 8 Account(String Bnumber,double YuE) 9 { 10 //初始化 11 this.Bnumber=Bnumber; 12 this.YuE=YuE; 13 } 14 15 16 17 //存款 18 //无返回值带参数的方法 19 double cun(double qianshu) 20 { 21 //验证 22 if(qianshu<0) 23 { 24 System.out.println("钱数错误!"); 25 26 }else 27 { 28 System.out.println("存了"+qianshu); 29 this.YuE+=qianshu; 30 } 31 return this.YuE; 32 } 33 //取款 34 // 35 void qu(double qianshu) 36 { 37 if(qianshu>YuE) 38 { 39 System.out.println("余额不足!"); 40 return; 41 } 42 System.out.println("取了"+qianshu); 43 this.YuE-=qianshu; 44 45 } 46 //有返回值无参数的方法 47 double getYuE() 48 { 49 50 return this.YuE; 51 } 52 53 54 55 }
1 package com.hanqi.z1p1; 2 3 public class Homework0516 { 4 5 public static void main(String[] args) { 6 // TODO 自动生成的方法存根 7 //主类中测试Account类的功能 8 //成员变量“账号”和“存款余额”,成员方法有“存款”、“取款”和“余额查询” 9 10 Account ac=new Account("100001",1000); 11 12 ac.cun(-100); 13 System.out.println("查询余额:"+ac.getYuE()); 14 ac.qu(300); 15 System.out.println("查询余额:"+ac.getYuE()); 16 17 18 19 } 20 21 }
运行结果:
时间: 2024-11-05 13:21:28