Java Novice Note 9

Source: Internet
Author: User

1.bank class

Package com.yfs.javase;//class encapsulates public class Bankcard {//attribute int balance;//default 0  instance variable assigned to each object one//string name;/Method Save Public void Savemoney (int money   ) {if (Money < 0) {System.out.println ("illegal operation ..."); return;} System.out.println ("Deposit to account" + Money + "Yuan ..."); balance + = money;}    The money  is called when the passed parameter public void Drawmoney (int money) {//Verifies the balance if (balance < money) {SYSTEM.OUT.PRINTLN ("Insufficient account balance ..."); Return Terminate method call} System.out.println ("Remove from account" + Money + "meta ..."); Balance-=-money;}}

2. Testing

Package Com.yfs.javase;public class Cardtest {public static void main (string[] args) {//  bankcard BC1 = null;//Declaration Reference//< C1/>BC1 = new Bankcard ();//Create object  bankcard bc1 = new Bankcard ();  SYSTEM.OUT.PRINTLN ("bank card balance:" + bc1.balance);  Bc1.drawmoney (200);//Call object Method    Bc1.savemoney ();    Bc1.drawmoney (500000);    Bc1.drawmoney (+);    Bc1.savemoney ( -500);  SYSTEM.OUT.PRINTLN ("bank card balance:" + bc1.balance);}  }

3. Test 1

Package Com.yfs.javase;public class CardTest2 {public static void main (string[] args) {  Bankcard bc1 = new Bankcard () ;  SYSTEM.OUT.PRINTLN ("bank card balance:" + bc1.balance);  Bc1.savemoney (the);  Bc1.drawmoney (+);  SYSTEM.OUT.PRINTLN ("bank card balance:" + bc1.balance);    Bankcard BC2 = new Bankcard ();  SYSTEM.OUT.PRINTLN ("bank card balance:" + bc2.balance);  Bc2.savemoney (+);  System.out.println ("BC2 Bank card balance:" + bc2.balance);  System.out.println ("BC1 Bank card balance:" + bc1.balance);}    

4. Test 2

Package Com.yfs.javase;public class CardTest3 {public static void main (string[] args) {  Bankcard bc1 = new Bankcard ();  Bc1.savemoney (the);  Bc1.drawmoney (+);  SYSTEM.OUT.PRINTLN ("bank card balance:" + bc1.balance);    Bankcard BC2 = new Bankcard ();  Bc2.savemoney (+);  System.out.println ("BC2 Bank card balance:" + bc2.balance);  Bc2.drawmoney (the);  Bc2.balance = 10000;//Modify the balance  unsafe Operation  Bc2.drawmoney (;  System.out.println ("BC2 Bank card balance:" + bc2.balance);}    

1.1bank class

Package com.yfs.javase.update;//class Wrapper public class Bankcard {//Property  private Property  can only be accessed privately in the current class balance;// An instance variable is assigned to each object by a private int password;//method save public void Savemoney (int money   ) {if (Cash < 0) {System.out.println ("illegal Operation ... "); return;} System.out.println ("Deposit to account" + Money + "Yuan ..."); balance + = money;//in method access}    //Fetch money  call when passed parameter public void Drawmoney (int Money) {//Verify balance if (Balance < money) {SYSTEM.OUT.PRINTLN ("Insufficient account balance ..."); return; Terminate method call} System.out.println ("Remove from account" + Money + "meta ..."); Balance-=-money;} public void  GetBalance () {System.out.println ("account balance:" + balance) cannot be modified by accessing the Balance property method;}

2.1 Testing

Package Com.yfs.javase.update;public class CardTest3 {public static void main (string[] args) {  Bankcard bc1 = new Bank Card ();  Bc1.savemoney (the);  Bc1.drawmoney (50000);//  bc1.balance = 100000000;//Compilation error//  System.out.println ("bank card balance:" + bc1.balance);// Private property cannot access   bc1.getbalance ();//Bc1.password;  }}

3.1 Method Overloading conditions

Package Com.yfs.javase.update;import Java.util.random;public class Demo2 {//public int add (int a, int b) {//return A + b;/ /}    //variable name cannot be overloaded condition//public int Add (int c, int d) {//return C + d;//}public double Add (int a, double b) {return a + B; }//return value type cannot be overloaded condition public long Add (int a, int b) {return a + B;} public static void Main (string[] args) {Demo2 D1 = new Demo2 (); Long val = D1.add (1,6);}}

4.1 Method Overloading Example

The overloaded method of the package Com.yfs.javase.update;public class Overridedemo {//method  uses the same name as public int add (int a, int b) {return a + B ;}    public  int  Add (int a, int b, int c) {return a + B + C;} Public double Add (double A, double b) {return a + B;} public static void Main (string[] args) {//create object Overridedemo od = new Overridedemo (); int val = Od.add (3,5); System.out.println ("Add (3,5) =" + val); val = Od.add (3,5,8);//The corresponding method is matched by the number of arguments passed in on invocation System.out.println ("add1 (3,5,8) =" + V AL);d ouble result = Od.add (3.14, 2.71); System.out.println ("Add (3.14, 2.71) =" + result);}}

Java Novice Note 9

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.