Java polymorphic Case Studies

Source: Internet
Author: User

Definition of polymorphism

The same thing, the different states at different times.

For example: Water may be in different states: gaseous, liquid, solid.

Second, the multi-state premise and embodiment

1. Having an inheritance relationship

2, there are methods to rewrite

3, there is a parent class reference to the Child class object

Three, the principle of compiling operation

1, compile to see the left, the parent class whether there is this method, no error.

2, run to see the right side, the result is generally the right sub-class rewrite results.

Four, case analysis

1  Packagepolymorphic;2 /**3 * polymorphic Case 1, basic4  * @authorZHONGFG5 * @date 2015-06-166  */7 InterfaceCar {8     9      Public AbstractString getName ();Ten      One      Public Abstract intGetPrice (); A } -  - classBenzImplementsCar { the  - @Override -      PublicString GetName () { -         //TODO auto-generated Method Stub +         return"BENZ"; -     } +  A @Override at      Public intGetPrice () { -         //TODO auto-generated Method Stub -         return300000; -     } -  - } in  - classBmwImplementsCar { to  + @Override -      PublicString GetName () { the         //TODO auto-generated Method Stub *         return"BMW"; $     }Panax Notoginseng  - @Override the      Public intGetPrice () { +         //TODO auto-generated Method Stub A         return250000; the     } +      - } $  $ classCarshop { -      -      Public voidcarinfo (car car) { theSystem.out.println ("Brand:" +car.getname () + ", Price:" +Car.getprice ()); -     }Wuyi } the  -  Public classPolymorphicdemo { Wu      Public Static voidMain (string[] args) { -          AboutCarshop cs =NewCarshop (); $         //The Benz object is passed . -Car car =NewBenz (); - cs.carinfo (car); -          A         //The object of the BMW is passed +Car =NewBMW (); the cs.carinfo (car); -          $         /** the * Operation Result: the * Brand: BENZ, Price: 300000 the * Brand: BMW, Price: 250000 the          */ -     } in}
View Code
1  Packagepolymorphic;2 /**3 * polymorphic Case 2, transformation issues4  * @authorZHONGFG5 * @date 2015-06-166  */7 Abstract classAnimal {8     9      Public Abstract voideat ();Ten } One  A classDogextendsAnimal { -      -      Public voideat () { theSystem.out.println ("Dog eats meat"); -     } -      -      Public voidLookdoor () { +System.out.println ("Dog janitor"); -     } + } A  at classCatextendsAnimal { -      -      Public voideat () { -System.out.println ("Cat Eats fish"); -     } -      in      Public voidPlaygram () { -System.out.println ("Cat Play Game"); to     } + } -  the  Public classPolymorphictest { *  $      Public Static voidMain (string[] args) {Panax Notoginseng         //in memory, it's a dog. -Animal A =NewDog (); the a.eat (); +         //Down Transformation ADog d =(Dog) A; the D.lookdoor (); +          -System.out.println ("------------------"); $          $         //in memory is the cat, the upward transformation -A =NewCat (); - a.eat (); the         //Down Transformation -Cat C =(Cat) A;Wuyi C.playgram (); the          -         //java.lang.ClassCastException, in-memory cats cannot be converted into dogs . Wu //Dog D2 = (dog) A; -          About         /** $ * Operation Result: - * Dog eats meat - * Dog Janitor -          * ------------------ A * Cat eats fish + * Cat play Games the          */ -     } $}
View Code

Java polymorphic Case Studies

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.