JAVA programming experience-Preliminary Design of Polymorphism

Source: Internet
Author: User

In the object-oriented thinking, encapsulation, inheritance, and polymorphism will be widely used in development. In addition to powerful functions, a robust system should also be highly scalable, polymorphism exactly applies this idea. Next, I will take Yang Xiaocong's way to a certain place as an example. We know that Yang Xiaocong needs to drive a vehicle if he wants to go somewhere first. Then, I first define a Driver Class responsible for driving.

Public class Driver {private String name; public String getName () {return name;} public void setName (String name) {this. name = name;} public void drive (Vihecle v) {v. go (this, new Address ("Wuhan "));}}

 

The above name represents the driver's name, drive represents how to drive, Vihecle represents the vehicle, because I do not know what transport is used, so we can define this class as abstract, we know that all means of transportation are driving methods, so GO is an abstract method. Here I only define it as an abstract method without concrete implementation, only pass in the driver's information and target address as parameters.
public abstract class Vihecle {      public abstract void go(Driver d,Address dest);  }  

 

Now that the Driver, driving method drive, and driving vehicle Vihecle are used, you need to define the destination Address. name indicates the destination name.
public class Address {      private String name;        public String getName() {          return name;      }        public void setName(String name) {          this.name = name;      }        public Address(String name) {          super();          this.name = name;      }        }  

 

Next, we can define the actual transportation tools. Here we take Car and Plane as examples. Both classes inherit the Vihecle transport, so I do not need to pass the value multiple times in actual calls.
Public class Car extends Vihecle {public void go (Driver d, Address dest) {System. out. println (d. getName () + "Driving" + dest. getName ());}}

 

Public class Plane extends Vihecle {@ Override public void go (Driver d, Address dest) {// TODO Auto-generated method stub System. out. println (d. getName () + "fly" + dest. getName ());}}

 

Finally, we can write a test class to test the results.
Public class Test {public static void main (String [] args) {Driver d = new Driver (); d. setName ("Yang Xiaocong"); d. drive (new Car (); d. drive (new Plane ());}}

 

Output result: Yang Xiaocong drove to Wuhan and Yang Xiaocong flew to Wuhan. Conclusion: in fact, we can also define methods based on each type of transport in the Driver class, however, this design concept is not flexible enough to modify this layer of code each time. Abstract The way we drive, we design for them from the standpoint of the customer. If the customer wants to drive, we will pass in the objects of the corresponding car. This method is highly scalable. If he still wants to train, we redefine a train class to inherit Vihecle, and we do not need to modify any logic in the Driver part. The three features of polymorphism include inheritance, rewriting, and reference of the parent class pointing to the subclass object.

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.