Java (15) polymorphic

Source: Internet
Author: User

I. Polymorphism

1.1 Polymorphism: Multiple forms of a thing

1.2 Manifestations of polymorphism

Behavior (method) Polymorphism: Overrides and overloads.

Referencing polymorphism (dynamic binding): This behavior is inconsistent at compile-time type and runtime

Example: Parent class = Subclass

Pet p = new Dog ();

Move up; parent class = Subclass

Example: Pet p = new Dog ();

Down Transformation: Subclass = (Subclass) Parent class

Example: Dog D = (dog) p;

1  Public Abstract classPet {2     PrivateString name;3     Private inthealth=100;4     Private intAge ;5     Private intLove=50;6      PublicPet () {7         8     }9      PublicPet (String name,intHealthintAgeintLove ) {Ten          This. Name =name; One          This. Health =Health ; A          This. Age =Age ; -          This. Love =Love ; -     } the      PublicString GetName () { -         returnname; -     } -      Public voidsetName (String name) { +          This. name=name; -     } +      Public intGethealth () { A         returnHealth ; at     } -      Public voidSethealth (intHealth ) { -          This. Health =Health ; -     } -      Public intGetage () { -         returnAge ; in     } -      Public voidSetage (intAge ) { to          This. Age =Age ; +     } -      Public intGetlove () { the         returnLove ; *     } $      Public voidSetlove (intLove ) {Panax Notoginseng          This. Love =Love ; -     } the      Public voidji () { +System.out.println ("Confession of the pet: My name is:" + This. name+ ", Age:" + This. age+ "and the intimacy of the host:" + This. Love); A     } the      Public Abstract voideat (); +}
Pet Abstract class
1  Public classDogextendspet{2     PrivateString strain;3      PublicDog () {4         5     }6      PublicDog (String name,intHealthintAgeintlove,string strain) {7         Super(name,health,age,love);8          This. Strain =strain;9     }Ten      Public voidCatchingflydisc () { OneSystem.out.println ("Pick Up Frisbee"); A         intHealth =gethealth (); -Health = health-10; - sethealth (health); the          -         intLove =Getlove (); -Love = Love+5; - setlove (Love); +     } -      Public voideat () { +System.out.println ("Dog Eats fish"); A     } at      PublicString Getstrain () { -         returnstrain; -     } -      Public voidSetstrain (String strain) { -          This. Strain =strain; -     } in  -}
Dog class inherit pet class
1  Public classPenguinextendsPet {2     PrivateString sex;3      PublicPenguin () {4         5     }6      PublicPenguin (String name,intHealthintAgeintlove,string Sex) {7         Super(name,health,age,love);8          This. Sex =sex;9     }Ten      Public voidswimming () { OneSystem.out.println ("Swimming"); A         intHealth =gethealth (); -Health = health-10; - sethealth (health); the          -         intLove =Getlove (); -Love = Love+5; - setlove (Love); +     } -      Public voideat () { +System.out.println ("Penguin Drink"); A     } at      PublicString Getsex () { -         returnsex; -     } -      Public voidsetsex (String sex) { -          This. Sex =sex; -     } in  -}
Penguin class inherit pet class
1  Public classMaster {2      Public voidfeed (Pet p) {3 p.eat ();4     }5      Public voidPlay (Pet p) {6         if(pinstanceofDog) {7Dog d =(Dog) p;8 D.catchingflydisc ();9}Else if(pinstanceofPenguin) {TenPenguin PE =(Penguin) p; One pe.swimming (); A         } -     } -}
Lord Human
1  Public classTESTPMPD {2      Public Static voidMain (string[] args) {3Master m=NewMaster ();4         //Down Transformation5Pet p =NewDog ();6Dog d =(Dog) p;7 M.feed (d);8 //Pet p = new Penguin ();9 //m.feed (p);Ten     } One}
Test Class

The result of the operation is:

Note:java.lang.ClassCastException: Type conversion exception

The run-time type of the parent class is inconsistent with the type of the subclass to be turned

Note: Inheritance relationships exist for referencing polymorphism

two. instanceof

Syntax: object a instanceof Type B

Determines whether the type of object A is type B, returns true, otherwise false

Example: D instanceof Dog

three. 2 different forms of polymorphism

A. Using a parent class as a method parameter to implement polymorphism

public void Play (Pet p) {...}

B. Using a parent class as a method return value to implement polymorphism

Public Pet getpet (int type) {...}

Java (15) polymorphic

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.