Why do you want to go back to styling

Source: Internet
Author: User

This program may seem a little strange. Why should everyone intentionally forget the type of an object? When you go back to modelling, you may have doubts about this. And it would seem simpler and more intuitive to have tune () simply get a wind handle and use it as its own variable. But note that if you do that, you need to write a new tune () for each type of instrument within the system. Suppose, according to the previous inference, to add stringed (strings) and brass (brass) instrument (musical instruments):
 

: Music2.java//overloading instead of Upcasting class Note2 {private int value;
  Private Note2 (int val) {value = val;}
public static final Note2 Middlec = new Note2 (0), CSharp = new Note2 (1), Cflat = new Note2 (2);

}//ETC.
  Class Instrument2 {public void play (Note2 N) {System.out.println ("Instrument2.play ()");
  Class Wind2 extends Instrument2 {public void play (Note2 N) {System.out.println ("Wind2.play ()");
  Class Stringed2 extends Instrument2 {public void play (Note2 N) {System.out.println ("Stringed2.play ()");
  Class Brass2 extends Instrument2 {public void play (Note2 N) {System.out.println ("Brass2.play ()");
  } public class Music2 {public static void tune (Wind2 i) {i.play (Note2.middlec);
  public static void Tune (Stringed2 i) {i.play (Note2.middlec);
  public static void Tune (Brass2 i) {i.play (Note2.middlec); public static void Main (string[] args) {Wind2 flute = new Wind2();
    Stringed2 violin = new Stringed2 ();
    Brass2 Frenchhorn = new Brass2 (); Tune (flute);
    No upcasting Tune (violin);
  Tune (Frenchhorn); }
} ///:~

This works, of course, but there is one big drawback: you must write a method that is closely related to the class for each new Instrument2 class. This means that the first time a much more programming volume is required. In the future, if you want to add a new method like tune () or add a new type to instrument, you still need to do a lot of coding work. Also, the compiler will not prompt for any errors even if you forget to overload your own method. As a result, the entire operation of the type is extremely difficult to manage and is in danger of getting out of control.
But wouldn't it be much simpler to write a method that would use the underlying class as a variable or parameter instead of using those specific derivative classes? That is, if we can ignore derived classes and just let our own code deal with the underlying class, the amount of work saved will be hard to estimate.
This is where the "polymorphism" is. However, most programmers (especially those with programmatic backgrounds) still seem unfamiliar with the workings of 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.