Java's multi-state explanation

Source: Internet
Author: User

Objective

What does polymorphism mean? Polymorphism is the kind of thing that can have many forms of expression.

Polymorphic three-factor

1. The passive party must have an inheritance relationship

2. Subclasses are generally overriding the parent class method

3. The parameters of the active party's function function must be set to the type of the passive party parent class

For example, a driver drives a car

If the driver can drive a car we need to define a car class and then add a car-driving method to the driver class.
If the driver can drive a truck, we need to define a truck class and then add a truck-driving method to the driver class. Below is a look at the traditional calling method code as follows:

Class Car{private string name;//non-parametric construction method public car () {}//has a parameter construction method public car (String name) {this.name = name;} public void SetName (String name) {this.name = name;} Public String GetName () {return this.name;} public void Run () {}}class bus extends Car{public bus () {}public bus (String name) {super (name);} public void Run () {System.out.println ("bus Running");}} Class Smallcar extends Car{public Smallcar () {}public smallcar (String name) {super (name);} public void Run () {System.out.println ("Coupe Running");}} Class drivers{//If the driver will open bus public void Driverbus (bus b) {b.run ();} If the driver will drive a sedan public void Driversmallcar (Smallcar smcar) {Smcar.run ();}} Class  testdt{public static void Main (string[] args) {Drivers d = new Drivers ();//driver to drive bus D.driverbus (new bus ("Bus"));d . Driversmallcar (New Smallcar ("Coupe"));}

As the above-mentioned in the addition of drivers to drive other cars will repeat the above behavior disadvantage is poor scalability, late code maintenance difficult.

Then we use polymorphic notation to optimize the above example code as follows:

Class Car{private string name;//non-parametric construction method public car () {}//has a parameter construction method public car (String name) {this.name = name;} public void SetName (String name) {this.name = name;} Public String GetName () {return this.name;} public void Run () {}}class bus extends Car{public bus () {}public bus (String name) {super (name);} public void Run () {System.out.println ("bus Running");}} Class Smallcar extends Car{public Smallcar () {}public smallcar (String name) {super (name);} public void Run () {System.out.println ("Coupe Running");}} Class Drivers{public void Drivercar (car car) {Car.run ();}} Class  testdt{public static void Main (string[] args) {Drivers d = new Drivers ();d. Drivercar (New bus ("Bus")); D.drivercar (New Smallcar ("Coupe"));}}

Compare the first one if the driver will drive another car, just add the class that will drive the car. The driver class does not need to add any additional function methods. Code introduction later maintenance is easier this form of writing polymorphism is also called upward transformation.

Java's multi-state explanation

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.