Java Classroom Assignments (vii)

Source: Internet
Author: User

First, the hands of the brain

1) Source 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); }}

2) Experiment:

3) Results Analysis:

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

This means that the object is a subtype, and it calls the method of the subtype, which is the parent type, and it calls the method of the parent type.

If the child class 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 fields in the subclass are accessed (not the fields in the parent class). If the subclass method does want to access a field that is hidden in the parent class with the same name, it can be accessed with the Super keyword.
If the subclass is used as a parent class, the field accessed through the subclass is the parent class!

Two. Hands-on Brain zoo (using interfaces instead of abstract base classes)

ImportJava.util.Vector; Public classZoo4 { Public Static voidMain (string[] args) {feeder F=NewFeeder ("Xiao Li"); Vector<Animal> ans =NewVector<animal>(); Ans.add (NewLion ());  for(inti = 0; I < 10; i++) {Ans.add (NewMonkey ()); }          for(inti = 0; I < 5; i++) {Ans.add (NewPigeon ());   } f.feedanimals (ANS); }}classFeeder { PublicString name; Feeder (String name) { This. Name =name; }     Public voidFeedanimals (vector<animal>ans) {         for(Animal An:ans) {an.eat (); }        }}InterfaceAnimal { Public Abstract voideat (); }classLionImplementsAnimal { Public voideat () {System.out.println (Lion "); }}classMonkeyImplementsAnimal { Public voideat () {System.out.println (Monkey "); }}classPigeonImplementsAnimal { Public voideat () {System.out.println (Pigeons "); }}

Three. Hands-on brain

ImportJava.util.*; Public classEx1115 { Public Static voidMain (string[] args) {Account a=NewAccount ("11111111111", "Dsdf", "20161115", "256398", 23.6);    A.jiemian (); }}classaccount{Private Static FinalString Joptionpane =NULL; Private  StaticString Id; Private StaticString name; Private StaticString date; Private StaticString Mima; Private Static DoubleExtra; Account (String i,string n,string d,string m,DoubleE//constructor Function{Id=i; Name=N; Date=D; Mima=m; Extra=e; }         Public voidset_id (String i) {Id=i; }     Public voidSet_name (String N) {name=N;; }     Public voidset_date (String d) {Date=D; }     Public voidSet_mima (String m) {Mima=m; }     Public voidSet_extra (Doublee) {Extra=e; }         PublicString get_id () {returnId; }     PublicString get_name () {returnname; }     PublicString get_date () {returndate; }     PublicString Get_mima () {returnMima; }     Public DoubleGet_extra () {returnExtra; }             Public Static voidJiemian ()//Interface{System.out.println ("Please select interface (1. Chinese 2. English):"); Scanner SC=NewScanner (system.in); String NUM1=Sc.nextline (); intnum11=Integer.parseint (NUM1); if(num11==1) {Scanner SC1=NewScanner (system.in); intnum=Integer.parseint (Sc1.nextline ());  while(true) {System.out.println ("Please select the action you want to take:"); System.out.println ("1. Withdrawals"); System.out.println ("2. Deposit"); System.out.println ("3. Transfer"); System.out.println ("4. Change Password"); System.out.println ("5. Check the balance"); System.out.println ("6. Return card"); if(num==1) Qukuan (); Else if(num==2) Cunkuan (); Else if(num==3) Zhuan (); Else if(num==4) Xiugai (); Else if(num==5) Chaxun (); Else if(num==6) {System.out.println ("Return card success!" "); }            }        }                Else{                    }        }         Public Static voidCunkuan ()//Deposit{System.out.println ("Please enter the deposit amount:"); Scanner SC=NewScanner (system.in); intJine =Integer.parseint (Sc.nextline ()); Extra+=Jine; System.out.println ("Your present amount is:" +extra); }         Public Static voidQukuan ()//Withdrawals{System.out.println ("Please enter the withdrawal amount"); String Output=""; Output+ = "+" + "\n500" + "\n1000" + "\n1500" + "\n2000" + "\n5000" + "\ n Other amount (Please enter 0)" + "\ n return operation (please enter-1)" + "\ nthe card (Please enter-2)";        SYSTEM.OUT.PRINTLN (output); Scanner SC=NewScanner (system.in); intnum=Integer.parseint (Sc.nextline ()); Switch(num) { Case100: {} extra-= 100; Break;  Case500: Extra-= 500; Break;  Case1000: Extra-= 1000; Break;  Case1500: Extra-= 1500; Break;  Case2000: Extra-= 2000; Break;  Case5000: Extra-= 5000; Break;  Case0: {System.out.println ("Please enter the withdrawal amount:"); Scanner SC1=NewScanner (system.in); intJine=Integer.parseint (Sc1.nextline ()); Extra-=Jine;            Sc1.close ();  Break; }         Case-1: System.out.println ("returned to operator interface"); Break;  Case-2: System.out.println ("Return card success!" "); Break; }    }         Public Static voidZhuan ()//Transfer Remittance{System.out.println ("Please enter the transfer line number:"); Scanner SC=NewScanner (system.in); String Hanghao=Sc.nextline (); Scanner SC1=NewScanner (system.in); intJine =Integer.parseint (Sc1.nextline ()); Extra-=Jine; System.out.println ("Successful transfer!" "); }         Public Static voidXiugai ()//Change Password{System.out.println ("Please enter the password you want to modify:"); Scanner SC1=NewScanner (system.in); String mi=Sc1.nextline (); Mima=mi; }         Public Static voidChaxun ()//Check Balance{System.out.println ("Balance is:" +extra); }    }

Java Classroom Assignments (vii)

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.