Java (polymorphic) hands-on brain

Source: Internet
Author: User

1>

See the following "Perverted" class (see Example Parentchildtest.java)

The code above is characterized by the following: Subclasses and parent classes define exactly the same fields and methods

Run the following test code

1. What is the result of the above program running?

2. How do you explain that you will get such output?

The first 100: is the parent call Printvalue method output the MyValue value in the parent class is 100;

The second 200: The child calls the Printvalue method to output the MyValue value in the child class to 200;

The third 200: is the parent = child, assigns the subclass object to the parents class, but at this time the child class method can only be called by the parent, when the subclass Printvalue is called, the output is of course the myvalue value of the subclass 200.

Fourth 200: Although the parent = child, the parent cannot call a variable of the subclass, so parent.myvalue++; just myvalue the variable in the parent class object to the value +1 but not the myvalue value of the subclass. But when the subclass Printvalue method is called, the output is the MyValue value of subclass 200.

Fifth 201: (child) parent, forced to convert parent to child object, when the parent is completely a child object, so the output value is 201.

3. The computer is not wrong, the reason why you get such a running result is also a reason, then from these running results, you can summarize the Java syntax features?

(1): When a subclass and parent class have a method of the same name, and a parent class variable refers to a subclass object, which method to invoke is determined by its own true type.

(2): If the subclass has the same field as the parent class, the fields in the subclass are substituted or hidden from the fields of the parent class, and the sub-class method accesses the fields in the subclass (not the fields in the parent class).

2>

The ATM operation process is simulated by polymorphic method.

ImportJava.util.Scanner;Abstract classatma{ Public Abstract voidQukuan ();  Public Abstract voidCunkuan ();  Public Abstract voidZhuanzhang ();  Public Abstract voidUpdatamima (String str);  Public Abstract voidShowmoney ();} Public classATM { Public Static voidMain (string[] args) {//TODO auto-generated Method StubAccount A =NewAccount ();    A.display (); }}classAccountextendsatma{String Account;    String name;    String data; intleixing;    String Mima; DoubleMoney ; Account () {} account (String account,string name,string data,string Mima,DoubleMoney ) {         This. Account =Account ;  This. Name =name;  This. data =data;  This. Mima =Mima;  This. Money =Money ; }     PublicString Getmima () {returnMima; }         Public voidMune () {System.out.println ("1. Withdrawals"); System.out.println ("2. Deposit"); System.out.println ("3. Transfer Remittance"); System.out.println ("4. Change Password"); System.out.println ("5. Check the balance"); System.out.println ("6. Return card"); }     Public voidQukuan () {System.out.println ("100 Yuan"); System.out.println ("500 Yuan"); System.out.println ("1000 Yuan"); System.out.println ("1500 Yuan"); System.out.println ("2000 Yuan"); System.out.println ("5000 Yuan"); System.out.println ("Other Amount"); System.out.println (Returns); Scanner in=NewScanner (system.in); System.out.println ("Please enter the withdrawal amount:"); intM =In.nextint (); if(M <=Money ) {System.out.println ("Withdrawing money, please later ... "); Money= Money-M; System.out.println ("Successful withdrawals!" "); }        Else{System.out.println ("Insufficient balance, withdrawal failed!" "); }    }     Public voidCunkuan () {Scanner in=NewScanner (system.in); System.out.println ("Please enter the deposit amount:"); intM =In.nextint (); Money= Money +M; System.out.println ("Deposit Success!" "); }     Public voidZhuanzhang () {Scanner in=NewScanner (system.in); System.out.println ("Please enter the transfer line number:"); String H=In.next (); System.out.println ("The name of the person you want to transfer is xxx?0: Yes, 1: No"); intx=In.nextint (); if(x==0) {System.out.println ("Please enter the transfer amount:"); intM =In.nextint (); if(Money >=M) {System.out.println ("Successful transfer!" "); Money= Money-M; }            Else{System.out.println ("Insufficient balance, transfer failed!" "); }        }        if(x==1) {System.out.println ("The card number is wrong!" "); }    }     Public voidUpdatamima (String str) {if(Str.equals (Mima)) {System.out.println ("The new password must not be duplicated with the old password!" "); }        Else{Mima=str; System.out.println ("Modified successfully!" "); }    }     Public voidShowmoney () {System.out.println ("Your account balance is:" +Money ); }     Public voiddisplay () {Scanner in=NewScanner (system.in); Account[] a=Newaccount[100]; a[0] =NewAccount ("123456789101", "CTT", "2016/11/15", "123456", 1000); System.out.println ("Please enter your password:" ); String input=In.next (); BooleanFlag =false;  for(inti = 0;i<50;i++)        {            if(A[i].getmima (). Equals (input) && a[i]!=NULL) Flag=true;  Break; }        intS1; if(flag) {Booleanp =true;  while(P) {mune (); System.out.println ("Please enter the action you want to perform:" ); S1=In.nextint (); if(S1 = = 1) {Qukuan (); Continue; }            if(S1 = = 2) {Cunkuan (); Continue; }            if(S1 = = 3) {Zhuanzhang (); Continue; }            if(S1 = = 4) {System.out.println ("Please enter a new password:"); String Str=In.next ();                Updatamima (str); Continue; }            if(S1 = = 5) {Showmoney (); Continue; }            Else{p=false; System.out.println ("Has exited!" "); }        }        }                Else{System.out.println ("Input Error! "); }    }}

Results:

Analysis: Using an abstract class implementation.

Java (polymorphic) hands-on brain

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.