1 PackageCOM.HANQI.Z1P1;2 3 Public classAccount {4 5 String Bnumber;6 DoubleYuE;7 8Account (String Bnumber,DoubleYuE)9 {Ten //Initialize One This. Bnumber=Bnumber; A This. Yue=YuE; - } - the - - //Deposit - //a method with no return value with parameters + DoubleCunDoubleQianshu) - { + //Validation A if(qianshu<0) at { -System.out.println ("The money is wrong!" "); - -}Else - { -System.out.println ("Saved" +Qianshu); in This. yue+=Qianshu; - } to return This. YuE; + } - //Withdrawals the // * voidQuDoubleQianshu) $ {Panax Notoginseng if(qianshu>YuE) - { theSYSTEM.OUT.PRINTLN ("Insufficient balance!") "); + return; A } theSystem.out.println ("took" +Qianshu); + This. yue-=Qianshu; - $ } $ //methods with no parameters for return values - DoubleGetyue () - { the - return This. YuE;Wuyi } the - Wu -}
1 PackageCOM.HANQI.Z1P1;2 3 Public classHomework0516 {4 5 Public Static voidMain (string[] args) {6 //TODO Auto-generated method stubs7 //test the function of the account class in the main class8 //member variables "account" and "Deposit balance", member methods are "deposit", "withdrawal" and "Balance Enquiry"9 TenAccount ac=NewAccount ("100001", 1000); One AAc.cun (-100); -System.out.println ("Check balance:" +Ac.getyue ()); -Ac.qu (300); theSystem.out.println ("Check balance:" +Ac.getyue ()); - - - + } - +}
Operation Result:
Encapsulation Class-saving money