Java language Static Multi-Dispatch and Dynamic single dispatch

Source: Internet
Author: User

Publicclass Cat {

publicvoid befeeded (person p) {

P.feed (this);

}

}

Publicclass Whitecat extends Cat {

publicvoid befeeded (person p) {

P.feed (this);

}

}

Publicclass Blackcat extends Cat {

publicvoid befeeded (person p) {

P.feed (this);

}

}

Publicclass person {

publicvoid Feed (cat cat) {

System.out.println ("Person feedcat");

}

publicvoid Feed (whitecat cat) {

System.out.println ("Person feedwhitecat");

}

publicvoid Feed (Blackcat cat) {

System.out.println ("Person feedblackcat");

}

}

Publicclass Man extends person {

publicvoid Feed (cat cat) {

System.out.println ("Man Feedcat");

}

publicvoid Feed (whitecat cat) {

System.out.println ("Man Feedwhitecat");

}

publicvoid Feed (Blackcat cat) {

System.out.println ("Man Feedblackcat");

}

}

Publicclass Woman extends person {

publicvoid Feed (cat cat) {

System.out.println ("Woman feedcat");

}

publicvoid Feed (whitecat cat) {

System.out.println ("Woman feedwhitecat");

}

publicvoid Feed (Blackcat cat) {

System.out.println ("Woman feedblackcat");

}

}

Publicclass Client {

A method consists of three parts: the recipient of the method, the name of the method, the parameter signature of the method (including the parameter type, the number of parameters, and the order of parameters).

Where the recipient of the method and the method signature are called the volume of the method

publicstaticvoid Main (String[]args) {

Test1 ()///Description Dynamic Dispatch

Test2 ()//Note Dynamic single dispatch

TEST3 ()//use inversion ball to achieve two times dynamic single dispatch

}

publicstaticvoid test1 () {

Cat cat = new cat ()//can be replaced with the following two statements, no effect on the result

Cat cat = new Blackcat ();

Cat cat = new Whitecat ();

Person p;

p = new person ();

P.feed (CAT);

p = New man ();

P.feed (CAT);

p = New Woman ();

P.feed (CAT);

The Java language invocation period has undergone a downward transition, which is dynamically assigned. Which is often called polymorphism.

Run Result:

Person Feed Cat

Mans Feed Cat

Woman Feed Cat

}

publicstaticvoid test2 () {

Cat cat = new cat ();

Cat BC = new blackcat ();

Cat WC = new whitecat ();

Person p;

p = new person ();

P.feed (CAT);

P.feed (BC);

P.feed (WC);

p = New man ();

P.feed (CAT);

P.feed (BC);

P.feed (WC);

p = New Woman ();

P.feed (CAT);

P.feed (BC);

P.feed (WC);

Regardless of any subtype of cat, the Java language takes into account only the recipient's volume, so it is a dynamic single assignment

Run Result:

Person Feed Cat

Person Feed Cat

Person Feed Cat

Mans Feed Cat

Mans Feed Cat

Mans Feed Cat

Woman Feed Cat

Woman Feed Cat

Woman Feed Cat

}

publicstaticvoid test3 () {

Cat cat = new cat ();

Cat Blackcat = new blackcat ();

Cat whitecat = new whitecat ();

Person p = new person ();

Person mans = New man ();

Person woman = new woman ();

Cat.befeeded (P);

Blackcat.befeeded (P);

Whitecat.befeeded (P);

Cat.befeeded (Mans);

Blackcat.befeeded (Mans);

Whitecat.befeeded (Mans);

Cat.befeeded (woman);

Blackcat.befeeded (woman);

Whitecat.befeeded (woman);

Departure two times Dynamic single dispatch

Person Feed Cat

Person Feed Blackcat

Person Feed Whitecat

Mans Feed Cat

Mans Feed Blackcat

Mans Feed Whitecat

Woman Feed Cat

Woman Feed Blackcat

Woman Feed Whitecat

}

}

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.