In the previous article we introduced about Java interface in this chapter we look at polymorphisms in Java classes.
In daily life, many do not mean we want to mean, as follows:
1, leadership: "What do you mean?"
Xiao Ming: "no meaning, the meaning of meaning."
Leadership: "You mean this is not enough."
Xiao Ming: "piece of cake, piece of cake."
Leadership: "You people are really interesting."
Xiao Ming: "In fact, no other meaning."
Leadership: "Then I would embarrassed."
Xiao Ming: "I was embarrassed."
2, the TV series "Get my brother away."
Sister: "You do not understand me."
Brother: "You do not say how I know you ah."
Sister: "Really understand the need to say it."
Brother: "You say I do not understand it yet?"
Sister: "What is the point ah say, I say you should not know."
Brother: "I'm not a fortune teller, I know you mean a few, ah,"
Sister: "I do not mean"
Brother: "really means nothing."
Sister: "Now I speak to you feel boring yet?"
Brother: "I say boring?"
Sister: "Then I said boring is boring thing?"
Brother: "Then you'd tell me what you mean in the end a few ah."
。。。
For different occupations, when we hear different words will think of different meanings, such as cut, knife surgery for doctors is, it is cut to tailor clothes for the actors is to suspend performances, which is a manifestation of a multi-state behavior, as well as water when we heard this object as a format can wash your face, you can cool down as solid as gas can be steamed buns, which is multi-state as a manifestation of an object.
Polymorphic:
1, meaning
1) When the same type of reference point to different objects have different behaviors ----- polymorphic behavior.
2) is shaped to the same object of different types have different performance ----- polymorphic objects.
2, shape up:
Reference 2.1) pointing object type parent subclass
2.2) can be cast to type are: parent type, implement.
2.3 type) can point out what, see references
3, cast, there are two conditions for success:
3.1) reference to the object pointed to is the type
3.2) a reference object points, the interface to achieve the change
4, through instanceof determines whether the reference is to a certain type, instanceof returns a boolean result, the success of its strong forwarding condition is true conditions.
1 abstract class people { 2 abstract void cut(); 3 } 4 5 class doctor extends people { 6 void cut() { 7 } // 做手术 8 } 9 10 class actor extends people { 11 void cut() { 12 } // 停止表演 13 } 14 15 class barber extends people { 16 voidCut () { . 17 } // Barber 18 }
In the above code, we are actually doing a polymorphic behavior codes for different types of people, cut () method behaves differently.
. 1 interface yetai { // liquid interfaces 2 void Xilian (); . 3 } . 4 . 5 interface Gutai { // solid interfaces . 6 void Jiangwen (); . 7 } . 8 . 9 interface Qitai { 10 void zhengmantou (); // gaseous interfaces 11 } 12 is 13 is class Water the implements yetai, Gutai, Qitai { 14 public void Xilian () { 15 } 16 . 17 public void Jiangwen () { 18 is } . 19 20 is public void zhengmantou () { 21 is } 22 is }
In the above code, we have created a liquid, gaseous, solid three interfaces, and each interface has its own way, then we pass the respective interface instance of water, it can be seen to achieve their different interface instance water, However, when not to visit another instance where the method, which is the same shape as the objects are different types have different performance, but if we have to call other interfaces in the way to do that, for example, in the above code in our non let yetai of water has jiagwen function, of course, does have such a function in real life,
We can achieve the above problems by means of forced conversion type. as follows:
. 1 public class the HelloWorld { 2 public static void main (String [] args) { . 3 yetai Water = new new Water (); . 4 Gutai Water1 = (Gutai) Water; . 5 water1.jiangwen (); . 6 } . 7 } . 8 . 9 interface yetai { // liquid interfaces 10 void Xilian (); . 11 } 12 is 13 is interface Gutai { // solid interfaces 14 void Jiangwen (); 15 } 16 . 17 interface Qitai { 18 is void zhengmantou (); // gaseous interfaces . 19 } 20 is 21 is interface qitai1 { 22 is void zhengmantou (); // gaseous Interface 23 is } 24 25 class Water the implements yetai, Gutai, {Qitai 26 is public void Xilian () { 27 } 28 29 public void Jiangwen () { 30 } 31 is 32 public voidzhengmantou () { 33 is } 34 is }
We gutai water1 = (gutai) water; the strong yetai water into gutai water, so that the function can be achieved jiangwen.