Java polymorphism [8.2.1 of programming ideas]

Source: Internet
Author: User
Tags export class

Associating a method call with the same method body is called a binding. If binding is implemented by the compiler and the linker before the program executes, it is called early binding. This is the default binding method that does not need to be selected in a process-oriented language. Now I am opposite the process language has been very vague, high school is only aware of the process vaguely remember what it used to be like Fox or Fox or something.

Binding according to the type of the object at run time, called late binding, also known as dynamic binding or runtime binding. If a language wants to implement late binding, it must have a mechanism to judge the type of the object at run time and invoke the appropriate method from the face. That is, the compiler has never known the object type, but the method invocation mechanism can find the correct method body, production to call. Late binding mechanisms vary depending on the language, as long as you think about it, you have to place some kind of "type information" in the object anyway.

In addition to the static and final methods in Java (the private method belongs to the final method), all other methods are late-bound. So all the methods in Java are polymorphic by dynamic binding, so you can write program code that deals with only the base class, and the code works correctly for all exported classes. or send a message to an object and let the object decide what to do.

The above example speaks:

Package com.ebao.java.dynamic.binding;

public class Shape {
public void Draw () {}
public void Erase () {}
}

public class Circle extends Shape {
public void Draw () {
System.out.println ("========circle.draw () = =");
}
public void Erase () {
System.out.println ("========circle.erase () = =");
}
}

public class Square extends Shape {
public void Draw () {
System.out.println ("========square.draw () = =");
}
public void Erase () {
System.out.println ("========square.erase () = =");
}
}

public class Trangle extends Shape {
public void Draw () {
System.out.println ("========trangle.draw () = =");
}
public void Erase () {
System.out.println ("========trangle.erase () = =");
}
}

Import Java.util.Random;

public class Randomshapegenerator {
Private random ran = new random (47);
Public Shape Next () {
Switch (Ran.nextint (3)) {
Default
Case 0:return New Circle ();
Case 1:return New Square ();
Case 2:return New Trangle ();
}
}
}

public class Testshape {
private static Randomshapegenerator Gen = new Randomshapegenerator ();
public static void Main (string[] args) {
Shape[] s = new shape[9];
for (int i=0;i<s.length;i++) {
S[i] = Gen.next ();
}
for (Shape shp:s)
Shp.draw ();
}
}

Operation Result:

========trangle.draw () = =
========trangle.draw () = =
========square.draw () = =
========trangle.draw () = =
========square.draw () = =
========trangle.draw () = =
========square.draw () = =
========trangle.draw () = =
========circle.draw () = =

Randomshapegenerator is a "factory" (factory) that, each time the next () method is called, it can produce a reference to a randomly selected shape object. Notice that the upward transformation occurs in the return statement. Each return statement obtains a reference to a circle, Square, triangle, and sends it from the next () method as a shape type. So no matter what the next () method is called, it is absolutely impossible to know exactly what the exact type is. Because we can always only get a generic shape reference. So random selection is a good indication that, at compile time, the compiler does not need to get any special information to make the correct call. All calls to the draw () method are made through dynamic binding.

Example two: Create a base class that contains two methods. A second method can be called in the first method. It then produces an export class that inherits from the class, overwriting the second method in the base class. Create an object for the export class, transform it up to the base class and call the first method.

Package com.ebao.java.dynamic.binding;

public class Shape1 {
public void Draw () {
System.out.println ("-------------------Shape1.draw ()");
This.erase ();
}
public void Erase () {
System.out.println ("-------------------shape1.erase ()");
}
}

public class TestShape1 extends Shape1 {
public void Erase () {
System.out.println ("-------------------testshape1.erase ()");
}
public static void Main (string[] args) {
Shape1 S1 = new TestShape1 ();
S1.draw ();
}
}

Operation Result:

-------------------Shape1.draw ()
-------------------Testshape1.erase ()

It can be seen that the upward transformation of the reference, regardless of the method of invocation is not covered by the exported class, the whole process as long as the method involved in the export class covered, dynamic binding is the method of exporting the class.

Java polymorphism [8.2.1 of programming ideas]

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.