The magic of Java polymorphism

Source: Internet
Author: User

Previously understood the basic idea, this version of the demonstration and the application is more real.

On the way down abstract methods and classes, interfaces, it is natural ...

Java files are put together, the file name of the previous post is compared to:

//////////////////////////// PackageCc.openhome; Public classRole {PrivateString name; Private intLevel ; Private intBlood;  Public intGetblood () {returnBlood; }   Public voidSetblood (intblood) {     This. Blood =Blood; }   Public intGetlevel () {returnLevel ; }   Public voidSetLevel (intLevel ) {     This. Level =Level ; }   PublicString GetName () {returnname; }   Public voidsetName (String name) { This. Name =name; }     Public voidfight () {//subclass Override this method;  }}////////////////////////////////////// PackageCc.openhome; Public classSwordsmanextendsRole {@Override Public voidfight () {System.out.println ("Attack with Swords"); }}///////////////////////////////////////////// PackageCc.openhome; Public classMagicianextendsRole {@Override Public voidfight () {System.out.println ("Attack with Magician"); }}/////////////////////////////////////////// PackageCc.openhome; Public classRPG { Public Static voidMain (string[] args) {Swordsman Swordsman=NewSwordsman (); Swordsman.setname ("Justin"); Swordsman.setlevel (1); Swordsman.setblood (200); System.out.printf ("Swordsman: (%s,%d,%d)%n", Swordsman.getname (), Swordsman.getlevel (), Swordsman.getblood ()); Magician Magician=NewMagician (); Magician.setname ("Monica"); Magician.setlevel (2); Magician.setblood (100); System.out.printf ("Magician: (%s,%d,%d)%n", Magician.getname (), Magician.getlevel (), Magician.getblood ());    Showblood (Swordsman);    Showblood (magician);    Drawfight (Swordsman);    Drawfight (magician); Role Role1=NewSwordsman (); Role Role2=NewMagician (); Swordsman SwordsMan1=(swordsman) Role1; Magician Magician1=(Magician) role2; Swordsman SwordsMan2=NewSwordsman (); Role Role3=swordsMan2; Swordsman SwordsMan3=(swordsman) Role3; }    /*Public static void Showblood (Swordsman swordsman) {System.out.printf ("%s Blood%d%n",  Swordsman.getname (), Swordsman.getblood ());                                           } public static void Showblood (Magician Magician) {System.out.printf ("%s Blood%d%n",  Magician.getname (), Magician.getblood ()); }  */     Public Static voidShowblood (role role) {System.out.printf ("%s Blood%d%n", Role.getname (), Role.getblood ()); }     Public Static voiddrawfight (role role) {System.out.print (Role.getname ());  Role.fight (); }}/////////////////////////////////

The magic of Java polymorphism

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.