Encapsulation Class-saving money

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.