A classic example of Java--------Dynamics

Source: Internet
Author: User

Polymorphic One:What is dynamic:Many different expressions of the same behavior, or many different implementations of the same behavior, are called polymorphism. 1) The relationship between Person,student,teacher
2) There is a "car" of the parent class, BMW inherited from the car, Mercedes-Benz inherited from the car.  In turn, the car can be BMW, or Mercedes-Benz, with different forms, this phenomenon is called polymorphic. For example, I want to go to the streets to buy something, please give me a car, then you pass to my car (a car object, formal parameter) An instance (argument) is polymorphic (can have different implementations, BMW, Mercedes, bicycles, etc. can be).
3) "Pen" is a parent class, and pens, pencils, and ballpoint pens are subclasses. At this time, if you give me a pen, you can give any pen.
RunRules:The run-time polymorphism that occurs after the method overrides is: The compile-time view of the data type, and the runtime to see the actual object type (new who tuned who).Polymorphic Two:Matching principle: First: Ensure that the parameters exactly match O, and then match this to super.
Again: Consider the parameter with super (O) asymptotic match o, matching this and super
When: There are subclasses overriding the parent class method, the class method is first toned according to the principle of the new subclass instance.
Match order: this.show (o) super.show (o)  this.show (Super (O)) super.show (Super ( O)) polymorphicClassic examples:
<span style= "FONT-SIZE:18PX;" > Match order: This.show (o),  super.show (o),                        this.show (Super (O)), Super.show (Super (O)) 1) First ensure that the parameters exactly match O, Match this to super in turn. 2) Then consider the parameter with super (O) asymptotic match O, and then match this and super3) when there are subclasses overriding the parent class method, the first tone class method is based on the principle of the new subclass instance. public class testdt{public  static void Main (String args[]) {       A a1 = new A ();        A A2 = new B ();          b b = new B ();        c C = new C ();        D d = new D ();           System.out.println (A1.show (b));  ①  A and a        System.out.println (A1.show (c));//  ②  A and a        System.out.println (A1.show (d ));  ③  A and D        System.out.println (a2.show (b));//  ④  B and A        System.out.println (A2.show ( c));  ⑤  B and A        System.out.println (a2.show (d));//  ⑥  A and D        System.out.println (B.show (b ));  ⑦  B and B        System.out.println (B.show (c));  ⑧  B and B        System.out.println (B.show (d));  ⑨  A and D  }</span>}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. I hope you have more advice

A classic example of Java--------Dynamics

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.