The use of polymorphism in Java

Source: Internet
Author: User

I. Hands-on brain

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

Results

Conclusion:

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

public class Zoo {public    static void Main (String args[]) {        feeder f = new Feeder ("Xiao Li");        Breeder Xiao Li feeds a lion        f.feedanimal (New Lion ());        Breeder Xiao Li feeds 10 monkeys for        (int i = 0; i < i++) {            f.feedanimal (new Monkey ());        }        Breeder Xiao Li feeds 5 pigeons for        (int i = 0; i < 5; i++) {            f.feedanimal (new Pigeon ());}        }    } Class Feeder {public    String name;    Feeder (String name)     {        this.name = name;    }        public void Feedanimal (Animal an)    {        an.eat ();    }} Abstract class Animal {public    abstract void Eat ();} Class Lion extends Animal {public    void eat ()    {        System.out.println ("I don't eat meat who dares to eat meat!") ");    }} Class Monkey extends Animal {public    void eat ()     {        System.out.println ("I eat everything, especially bananas. ");    }} Class Pigeon extends Animal {public        void eat ()     {        System.out.println ("I want to lose weight, so eat only a little rice every day.") ");    }}

  

Implementation of the ATM machine:

Import Java.util.scanner;public class ATM {private static string name;private static string Data;private static string  Mima;  static double money; Static ATM Code=new ATM ("Ye Zhihao", "11.13", "123456", "n"), Static Scanner str=new Scanner (system.in);p ublic atm (String name1 , String data1,string mima1,double money1) {name=name1;data=data1;mima=mima1;money=money1;} /*public static void Qu () {System.out.println ("Select Access amount" + "\ n" + "1, 100 yuan" + "\ n" + "2", "+" + "\ n" + "3," + "+" \ n "+" 4, "+" \ n ") + "5," + "\ n" + "6, +" + "\ n" + "7, other Amount"); int caozuo=str.nextint (); if (caozuo==1) {if (money<100) {System.out.prin    TLN ("Insufficient balance");    Qu (); }money=money-100; System.out.println ("balance" +money);}    if (caozuo==2) {if (money<500) {System.out.println ("insufficient balance");    Qu (); }money=money-500; System.out.println ("balance" +money);}    if (caozuo==3) {if (money<1000) {System.out.println ("insufficient balance");    Qu (); }money=money-1000; System.out.println ("balance" +money);} if (caozuo==4) {if (money<1500) {System.out.println ("balanceInadequate ");    Qu (); }money=money-1500; System.out.println ("balance" +money);}    if (caozuo==5) {if (money<2000) {System.out.println ("insufficient balance");    Qu (); }money=money-2000; System.out.println ("balance" +money);}    if (caozuo==6) {if (money<5000) {System.out.println ("insufficient balance");    Qu (); }money=money-5000; System.out.println ("balance" +money);}    if (caozuo==7) {double money1=str.nextdouble (); Money=money-money1; System.out.println ("balance" +money);}} */public static void Qu () {System.out.println ("Select Access amount" + "\ n" + "1, 100 yuan" + "\ n" + "2", "+" + "\ n" + "3," + "+" \ n "+" 4, "+" \ n ") + "5," + "\ n" + "6," + "+" \ n "+" 7, other Amount "); int caozuo=str.nextint (); Qu a=new Qu (); if (caozuo==1) {Qu100 b=new Qu100 (); A=b;b.qu (code); System.out.println ("balance" +money);} if (caozuo==2) {Qu500 b=new Qu500 (); A=b;b.qu (code); System.out.println ("balance" +money);} if (caozuo==3) {Qu1000 b=new Qu1000 (); A=b;a.qu (code); System.out.println ("balance" +money);} if (caozuo==4) {Qu1500 b=new Qu1500 (); A=b;a.qu (code); System.out.println ("balance" +money);} if (caozuo==5) {Qu2000 b=new Qu2000 (); a=b;a.qu(code); System.out.println ("balance" +money);} if (caozuo==6) {Qu5000 b=new Qu5000 (); A=b;a.qu (code); System.out.println ("balance" +money);}} public static void Zhuan () {System.out.println ("Enter the card number to be transferred"); String Ka=str.next (); System.out.println ("Enter the amount to be transferred");d ouble money1=str.nextdouble (); money=money-money1; System.out.println ("successful Transfer");} public static void Cun () {System.out.println ("Input access Amount");d ouble money1=str.nextdouble (); money=money+money1; System.out.println ("balance" +money);} public static void Xugai () {System.out.println ("Enter Password to be modified"); String Mima1=str.next (); mima=mima1;} public static void Main (String args[]) {System.out.println ("Enter password"); String Mima1=str.next (), if (Mima1.equals (Atm.mima)) {int X=0;while (x!=5) {if (x==1) {qu ();} if (x==2) {cun ();} if (x==3) {Zhuan ();} if (x==4) {Xugai ();} if (x==5) {System.out.println ("User name:" +name+ "Date:" +data+ "Balance:" +money "); System.out.println ("select operation"); System.out.print ("1. Withdrawals" + "\ n" + "2. Deposit" + "\ n" + "3. Transfer" + "\ n" + "4. Change password" + "\ n" + "5. Return card"); int w=str.nextint (); x=w;} else {System.out.println ("bad password");}}} Class Qu{public void Qu (atm a) {}}class Qu100 extends qu{public void qu (atm a) {a.money=a.money-100;}} Class Qu500 extends Qu{public void Qu (ATM a) {a.money=a.money-500;}} Class Qu1000 extends Qu{public void Qu (ATM a) {a.money=a.money-1000;}} Class Qu1500 extends Qu{public void Qu (ATM a) {a.money=a.money-1500;}} Class Qu2000 extends Qu{public void Qu (ATM a) {a.money=a.money-2000;}} Class Qu5000 extends Qu{public void Qu (ATM a) {a.money=a.money-5000;}}

Results

The use of polymorphism in Java

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.