JAVA-08 polymorphism and abnormal handling

Source: Internet
Author: User

1. Run the following code:

 Public classParentchildtest { Public Static voidMain (string[] args) {Parent parent=NewParent ();        Parent.printvalue (); Child Child=NewChild ();                Child.printvalue (); Parent=Child ;                Parent.printvalue (); Parent.myvalue++;                Parent.printvalue (); ((child) parent). MyValue++;            Parent.printvalue (); }}classparent{ Public intmyvalue=100;  Public voidPrintvalue () {System.out.println ("Parent.printvalue (), myvalue=" +myvalue); }}classChildextendsparent{ Public intmyvalue=200;  Public voidPrintvalue () {System.out.println ("Child.printvalue (), myvalue=" +myvalue); }}

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

The first is the parent class's own object, and the parent class's own method is called. The second is a subclass of its own object that calls its own method.

The third parent class variable refers to a subclass object that invokes a method of the subclass. The fourth object that references the subclass, invokes the method of the subclass.

The fifth one changes the subclass's variables by type conversion, using the subclass method.

When a subclass has the same method as the parent class, and a parent class variable refers to a subclass object, which method is called, determined by the object's own "true" type, that is, the object is a subtype, it calls the method of the subtype, it is the parent type, and it invokes the method of the parent type.

2.

ImportJava.util.Scanner;classaccount{String Account;    String name;    String date; intkey; DoubleMoney ;  PublicAccount (String a,string b,string C,intDDoublee) { account=A; Name=b; Date=C; Key=D; Money=e; }    } Public classOption { Public Static voidMain (string[] args) {account AA=NewAccount ("622888040311", "Zxw", "2016.11.15", 123456,1000); intb; System.out.println ("Please select language: 1. Chinese 2. English"); Scanner in=NewScanner (system.in); A=In.nextint (); System.out.println ("Please enter your password:"); Scanner in1=NewScanner (system.in); b=In1.nextint (); if(b==Aa.key) {             while(true){                            intChoose; System.out.println ("Please select the operation: 1. Deposit 2. Withdrawals 3. Transfer 4. Change Password 5. Check balance"); Scanner in2=NewScanner (system.in); Choose=In2.nextint (); if(choose==1) {System.out.println ("Please enter the deposit amount:"); Scanner in3=NewScanner (system.in); Doublecun=In3.nextint (); Aa.money=aa.money+Cun; Continue; }            Else if(choose==2) {System.out.println ("Please enter the withdrawal amount:"); Scanner in4=NewScanner (system.in); Doublequ=in4.nextdouble (); if(aa.money<qu) {System.out.println ("Insufficient balance in the card"); } Aa.money=aa.money-Qu; Continue; }            Else if(choose==3) {System.out.println ("Please enter the card number to transfer:"); Scanner in6=NewScanner (system.in); String Hang=In6.nextline (); if(Hang.equals (Aa.account)) {System.out.println ("Please enter the transfer amount:"); Scanner In5=NewScanner (system.in); Doublezhuan=in5.nextdouble (); Aa.money=aa.money-Zhuan; }            }            Else if(choose==4) {System.out.println ("Please enter the original password:"); Scanner in7=NewScanner (system.in); intMi=In7.nextint (); if(mi==Aa.key) {System.out.println ("Please enter a new password:"); Scanner In8=NewScanner (system.in); intMi2=In8.nextint (); Aa.key=mi2; }            }            Else if(choose==5) {System.out.println ("The current balance is:" +Aa.money); }                        }        }        ElseSystem.out.println ("The password is wrong, the card is locked and cannot be manipulated"); }}

JAVA-08 polymorphism and abnormal handling

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.