Java Note polymorphism

Source: Internet
Author: User

Listen to a classmate today to tell a good example of Java dynamic binding, I am very inspired, hereby recorded. Java dynamic binding is not tied at runtime, but not at compile time, which provides great convenience for the flexibility of the code, and makes it easier to maintain edges.


For dynamic bindings my understanding is that when defining a method, some parameters, such as a class object, are required, but this object is determined when it is run, such as the various guns when you hit CS, and it becomes tedious to overload different object parameters for this method, so we can build a gun's parent class, Upload the Parent object to this parameter, and at run time, decide what kind of gun it is.

Class Gun {public void shot () {System.out.println ("gun Shot");}} Class Pistol extends Gun {public void shot () {System.out.println ("Pistol Shot");}} Class Rifle extends Gun {public void shot () {System.out.println ("Rifle shot");}} Class Terrist {private Gun mygun; Terrist (gun) {mygun = gun;} public void Pulltrigger () {mygun.shot ();}} public class Latebind{public static void Main (string[] args) {terrist T1 = new Terrist (new Gun ()); T1.pulltrigger (); Terrist t2 = new Terrist (new Pistol ()); T2.pulltrigger (); terrist t3 = new Terrist (new Rifle ()); T3.pulltrigger ();}    }

  

Define a parent class called the gun, it has a method called shot (), the output is "gun shot", in the definition of a subclass, pistol (Shou gun), Rifle (Rifle), each of them rewrite the method of the parent class, output "Pistol shot" and "Rifle shot". In defining a terrist, there is a method Pulltrigger () that will invoke the shot method of your own gun. We know that the shot of different guns are different, but if you overload a Pulltrigger () method for each gun, the code is redundant. But we know that when the parent class appears, the subclass can appear, so we declare the gun object as the gun type, so we can put any of the gun's subtypes into it, but what about the effect of the subclass? The output is: Gun shot
Pistol shot Rifle shot, sub-class work very well! What is this for? In fact, the Java method is stored in the code area, the parent class's shot method exists there, the subclass of the overriding shot method is also stored there, when you rewrite the parent class method, the subclass will point to the subclass implementation of the shot method, that is, according to the type of the current object to execute the corresponding method.

In fact, the gun class here can be replaced by abstract classes or interfaces, assuming we use interfaces. When a subclass implements the shot method of the gun, we use the interface's Reference object to access the implementation class, which exposes the methods implemented by the implementation class to operate. At the same time, when we need to add a new weapon RPG, only need to let the RPG implementation of the Gun interface, where the gun appears, RPG can appear, we only need to define the RPG this class.

Above is a little humble opinion, is very superficial understanding, there is a long way to go!

Java Note polymorphism

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.