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